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 7849143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:26:24+00:00 2026-06-02T18:26:24+00:00

I am new to threading. Here if I comment pthread_join( thread1, NULL) then in

  • 0

I am new to threading.
Here if I comment pthread_join( thread1, NULL) then in the output sometimes I get

    Thread2
    Thread1
    Thread1

I am not able to understand why Thread1 trace is coming twice and what is the exact functionality of pthread_join.

Also, please refer some tutorial on threading concepts for beginners.

    void *print_message_function( void *ptr );
    main()
    {
            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);

            pthread_join( thread2, NULL); 

            printf("Thread 1 returns: %d\n",iret1);
            printf("Thread 2 returns: %d\n",iret2);
            exit(0);
    }

    void *print_message_function( void *ptr )
    {
            char *message;
            message = (char *) ptr;
            printf("%s \n", message);
    }
  • 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-06-02T18:26:25+00:00Added an answer on June 2, 2026 at 6:26 pm

    If I am getting these results, first of all I would do the following:

    1) Instead of below lines,

    iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
    
    iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
    pthread_join( thread1, NULL);
    
    pthread_join( thread2, NULL); 
    

    replace it with,

    iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
    
    pthread_join( thread1, NULL);
    iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
    
    pthread_join( thread2, NULL); 
    

    and see what is the result.

    2) Inside your thread function, you need to call pthread_exit(“Exit”); This is a proper way to exit from the thread function. Do it at the end of function.

    void *print_message_function( void *ptr )
    {
            char *message;
            message = (char *) ptr;
            printf("%s \n", message);
            pthread_exit("Exit"); 
        }
    

    If you are doing it this way, ideally you should not face any problem. In every case, i am assuming you are compiling your program using gcc -D_REENTRANT -o threadex threadex.c -lpthread

    This is not the final solution. If it is going well, then we can proceed to next step of starting both the threads at a time.

    Please share the feedback after incorporating these changes.

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

Sidebar

Related Questions

I'm trying to create a new System.Threading.Thread object using Jscript, but I can't get
I am new to mutli-threading in java and trying to understand the keyword synchronization
Would anyone be able to help me here please. I'm fairly new to VB.net
I'm trying to get started with learning threading in Java and here's a simple
I am very new to WPF. And just started learning threading. Here is my
I am relatively new to multi-threading and want to execute a background task using
I am new to the concept of threading in C, so I find it
I'm new to C#, RestSharp, and threading, so heres what I'm trying to do:
New Question I am looking for a way in Javascript to get the parent
new_story GET /story/new(.:format) {:action=>new, :controller=>stories} edit_story GET /story/edit(.:format) {:action=>edit, :controller=>stories} story GET /story(.:format) {:action=>show,

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.