0

Would any one please tell me what happens between the last two code lines

// Creating Server and Client threads
pthread_create(&serverThread, NULL, (void* (*)(void*))&Server,(void *)0);

pthread_create(&clientThread, NULL, (void* (*)(void*))&Client,(void *)1);

// Wait until serverThread exits
pthread_join( serverThread, NULL);

// Wait until clientThread exits
pthread_join( clientThread, NULL);

I want to wait them simultaneously. What if one of the two threads terminates/exits? What if server kept running in an infinite loop?

  • 1
    please tell us what you error is. – Irrational Person Jan 11 '15 at 05:41
  • I am not talking about an error :) My question is: What happens between the last two code lines? – محمد جعفر نعمة Jan 11 '15 at 05:43
  • 1
    possible duplicate of [pthreads - Join on group of threads, wait for one to exit](http://stackoverflow.com/questions/4577471/pthreads-join-on-group-of-threads-wait-for-one-to-exit) – user3553031 Jan 11 '15 at 05:44
  • 1
    look here http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html#CREATIONTERMINATION. Aslo please compile your code first. – Irrational Person Jan 11 '15 at 05:50
  • 1
    What if an asteroid hits Earth between the first and the second call to `pthread_join`? Please explain what kind of treat you are looking to avoid by simultaneos waiting. A thread can terminate, that's what most threads do, `pthread_join` lets you wait for exactly that. The other one is unclear. How simultaneous waiting would ket you recover from an infinite loop? – n. m. could be an AI Jan 11 '15 at 05:50

1 Answers1

1

The first join - pthread_join(serverThread, NULL); will wait until serverThread terminates.

The clientThread may or may not terminate during this time; if it terminates, it remains in zombie state until pthread_join(clientThread, NULL); gets called. pthread_join will return immediately in this case.

If clientThread has not yet finished execution when pthread_join(clientThread, NULL); is called, it will wait again until clientThread terminates.

nav
  • 1,645
  • 15
  • 22