0

I am currently making intern and I am asked to write a multi client server-client application with using C++. Hence, I'm trying to learn threading. Have one question:

I want to print "you are in thread A", then "you are in thread B", "now you are again in thread A". However it only prints first two sentences and ignores endl command. Can't exactly understand how it works. How to fix that and could you briefly explain working mechanism?

Why main thread exits before all function calls completed?

void  * function1(void * arg);
void  * function2(void * arg);


pthread_t thr_A, thr_B;
int main( void )
{

    pthread_create(&thr_A, NULL, function1, (void*)thr_B); 
    pthread_create(&thr_B, NULL, function2,NULL); 

return 0;

}

void * function1(void * arg)
{

  cout << "You are in thread A" << endl;
  pthread_join(thr_B, NULL);
  cout << "now you are again in thread A" << endl; 
  pthread_exit((void*)thr_A);


}

void * function2(void * arg)
{
    cout << " you are in thread B "  << endl ;
    pthread_exit((void*)thr_B);
}
1
  • 3
    If you've been asked to use C, then why are you using C++? And if you're using C++, why aren't you using the standard thread library? Commented May 29, 2013 at 11:59

1 Answer 1

1

In you main function you create one race condition. The threads may be started in any order, unless you specifically synchronize your code so that you enforce one or the other to start. Therefore it is also impossible to tell which will finish first. Then you also have your main thread, it might even finish before the threads you create finish. When using pthreads you must call pthread_join in order to wait for a thread to finish. You can do that like this:

int main( void )
{
    // you pass thread thr_B to function one but 
    // function2 might even start before function1
    // so this needs more syncronisation
    pthread_create(&thr_A, NULL, function1, (void*)thr_B); 
    pthread_create(&thr_B, NULL, function2,NULL); 

    //this is mandatory to wait for your functions
    pthread_join( thr_A, NULL);
    pthread_join( thr_B, NULL);

    return 0;

}

in order to wait in function1 you need more sophisticated synchronization method for example see for example pthread_cond_wait pthread_cond_signal as explained in: https://computing.llnl.gov/tutorials/pthreads/#ConVarSignal You also should remove the pthread_join from function one because according man pthread join: "If multiple threads simultaneously try to join with the same thread, the results are undefined."

Edit on comment of David hammen:

void * function1(void * arg)
{

  cout << "You are in thread A" << endl;
  //Remove the next line and replace by a pthread_cond_wait.
  pthread_join(thr_B, NULL);
  cout << "now you are again in thread A" << endl; 
  pthread_exit((void*)thr_A);

}
Sign up to request clarification or add additional context in comments.

4 Comments

That's exactly what I want. Thank you.
@nihirus As noted by mike above if decide to use C++ instead of C have a look at this tutorial. devx.com/SpecialReports/Article/38883 It might save some work and is much better suited for C++, although when used properly pthreads will do the job. If you think my post was very helpful you might want to accept my answer for future users.
The posted code is undefined behavior: The behavior [of pthread_join()] is undefined if the value specified by the thread argument to pthread_join() does not refer to a joinable thread. In this case, thr_B is not joinable in main() because function1 has already called pthread_join(thr_B, NULL); The solution is to either get rid of the second call to pthread_join() in this answer.
@DavidHammen I think I mentionned this issue in the last line of my post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.