Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4092376
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:29:02+00:00 2026-05-20T19:29:02+00:00

I have a question about some code I’m testing to start understanding posix threads.

  • 0

I have a question about some code I’m testing to start understanding posix threads.

I have this basic code:

#include <iostream>
#include <string>
#include <sstream>
#include <pthread.h>

using namespace std;

void *printInfo(void *thid){
    long tid;
    tid =(long)thid;
    printf("Hello from thread %ld.\n",tid);
    pthread_exit(NULL);
}

int main (int argc, char const *argv[])
{
    int num =8;
    pthread_t threadlist[num];
    int rc;
    long t;
    for(t=0;t<num;t++){
        printf("Starting thread %ld\n",t);
        rc = pthread_create(&threadlist[t],NULL,printInfo,(void *)t);
        if(rc)
        {
            printf("Error creating thread");
            exit(-1);
            
        }
    }
    pthread_exit(NULL);
    return 0;
}

Very simple code, start threads and print from them, this all works wonders, except that I don’t understand the last pthread_exit(NULL) before the return 0; in the end of the main method.

It seems that the main thread should not be a pthread, and should not need that! If I don’t put it, the code does not work: the code compiles and executes, but I only get the "starting thread" print messages, and not the "hello from …" messages.

So basically I want to know why is that needed.

Also, the code compiles and executes properly if I comment out the include <psthread.h>.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T19:29:02+00:00Added an answer on May 20, 2026 at 7:29 pm

    If you don’t use pthread_exit in the main function then all created threads are terminated as main finishes, i.e. in your case before they have printed anything.

    By calling pthread_exit in the main function makes main wait until all threads have completed.

    From the pthread_exit man page:

    The process will exit with an exit status of 0 after the last thread has been terminated. The behavior is as if the implementation called exit() with a zero argument at thread termination time.

    This is referring to calling pthread_exit() from the main processes thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick question about include/requre_once . I have some code that is common to a
I have a question about the following code: #include <iostream> #include <boost/scoped_ptr.hpp> class Interface
i have what i hope is a quick question about some code i am
I'm working on some JQuery code and I have a question about caching selectors.
I have some question about assembly, is it possible to convert ASCII code of
I have a question about interface and class implementing interface. This is my code:
Hello i have some question about java. here is my code: public static void
I was looking at some code that uses Boost.Function and have a question about
I have a configuration question about SVN. I have downloaded some code from a
I have a question due to some doubts about some of my code. In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.