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);
}