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