Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6754243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:15:24+00:00 2026-05-26T13:15:24+00:00

edit: I made the wrong assumption that threads started running on pthread_join when they

  • 0

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


I’m learning to use Posix threads, and I’ve read that:
pthread_join() - wait for thread termination

So, in the code sample, main’s exit(0) is not reached until both started threads end.
But after the first call to pthread_join(), main continues executing, because the second call to pthread_join() actually runs, and the message in between is printed.
So how’s this? does main continue executing while both threads aren’t finished yet? or doesn’t it?
I know this isn’t a reliable way of testing, but the second test message always gets printed after both threads are finished, no matter how long the loop is. (at least on my machine when I tried it)

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:15:25+00:00Added an answer on May 26, 2026 at 1:15 pm

    The function pthread_join waits for the thread to finish or returns immediately if the thread is already done.

    So in your case

    pthread_join( thread1, NULL); /* Start waiting for thread1. */
    printf( "Let's 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 executing,
    because the second call to pthread_join() actually runs, and the
    message in between is printed.

    False. pthread_join waits unless thread1 is already done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: I had made a mistake during the debugging session that lead me to
EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
Greetings I made the following php script so that I could edit text and
EDIT Taking COST 100 out made the command go through, however, I'm still unable
-edit- nevermind, i made a mistake in my select statement. Update was working correctly.
IMPORTANT EDIT: Sorry everyone, i made a big mistake in the structure. char *name;
Edit: From another question I provided an answer that has links to a lot
i have made a file open dialog, it contains an edit control whose variable
here I uploaded my Android project made in Eclipse. The idea is that i
I have a class that I made for some unit tests. Everything was going

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.