Does pthread_join () execute or not execute execution when a thread is called to continue?

edit I made the wrong assumption that threads started working on pthread_join when they actually started working on pthread_create .


I am learning to use Posix threads and I read that:
pthread_join() - wait for thread termination

So, in the sample code, the main output (0) is not reached until both ends of the stream have ended.
But after the first call to pthread_join (), main continues execution, because the second call to pthread_join () is actually executed, and a message is printed between them.
So how is it? Does main continue execution until both threads are finished? or not?
I know that this is not a reliable way of testing, but the second test message is always printed after the completion of both threads, no matter how long the cycle takes. (at least on my machine when I tried)

 void *print_message_function( void *ptr ) { char *message = (char *) ptr; for( int a = 0; a < 1000; ++a ) printf( "%s - %i\n", message, a ); return NULL; } // int main( int argc, char *argv[] ) { pthread_t thread1, thread2; char message1[] = "Thread 1"; char message2[] = "Thread 2"; int iret1, iret2; // iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1); iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2); // pthread_join( thread1, NULL); printf( "Let see when is this printed...\n" ); pthread_join( thread2, NULL); printf( "And this one?...\n" ); // printf("Thread 1 returns: %d\n",iret1); printf("Thread 2 returns: %d\n",iret2); exit(0); } 
+4
source share
4 answers

The pthread_join function waits for the thread to finish or return immediately if the thread has already completed.

So in your case

 pthread_join( thread1, NULL); /* Start waiting for thread1. */ printf( "Let see when is this printed...\n" ); /* Done waiting for thread1. */ pthread_join( thread2, NULL); /* Start waiting for thread2. */ printf( "And this one?...\n" ); /* Done waiting for thread2. */ 

But after the first call to pthread_join (), main continues execution, because the second call to pthread_join () actually executes, and there is a message between them.

False pthread_join waits if thread1 has already failed.

+6
source
 pthread_join( thread1, NULL); 

The main thread waits for this call here until thread1 finishes its work. After thread1 completes, the main thread continues and executes the next printf statement.

 printf( "Let see when is this printed...\n" ); 

Again, Main thread will wait here until thread2 completes its work.

 pthread_join( thread2, NULL); 

As soon as thread2 completes its job, the main thread moves forward, and the next statement, which is printf , is executed.

 printf( "And this one?...\n" ); 

The sequence will work as above. It probably happens too soon that the footprints you see make it confusing.
Also, not using printf to view the behavior of multi-threaded programs can be quite misleading, the order of printf may not always indicate the correct control flow. Since this synchronization and flushing of buffers in stdout may not be performed in the sasme order since fingerprints were performed on threads.

+3
source

pthread_join() does not return (blocks the calling thread) until the thread to be connected is complete. If the thread has already completed, it returns immediately.

In your test, both threads do exit, and therefore, of course, you will see all messages printed from the main thread. When the first message is printed, you know that thread1 complete; when the second is printed, you know that thread2 also complete. This is likely to happen pretty quickly after the first, since both threads do about the same amount of work at about the same time.

+3
source

If the first pthread_join returns immediately, this assumes that the first thread has already completed execution. What does the conclusion look like? Do you see any output "Thread 1 - n" after "See when it will be printed"?

+2
source

All Articles