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

  • SEARCH
  • Home
  • 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 7956039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:52:30+00:00 2026-06-04T03:52:30+00:00

I read a book, which give the next code: void *printme(void *id) { int

  • 0

I read a book, which give the next code:

void *printme(void *id) {
    int *i;
    i = (int *)id;
    printf("Hi. I'm thread %d\n", *i);
    return NULL;
}

void main() {
    int i, vals[4];
    pthread_t tids[4];
    void *retval;
    for (i = 0; i < 4; i++) {
        vals[i] = i;
        pthread_create(tids+i, NULL, printme, vals+i);
    }
    for (i = 0; i < 4; i++) {
        printf("Trying to join with tid%d\n", i);
        pthread_join(tids[i], &retval);
        printf("Joined with tid%d\n", i);
    }
}

and the next possible output:

Trying to join with tid0 
Hi. I'm thread 0 
Hi. I'm thread 1 
Hi. I'm thread 2 
Hi. I'm thread 3 
Joined with tid0 
Trying to join with tid1 
Joined with tid1 
Trying to join with tid2 
Joined with tid2 
Trying to join with tid3 
Joined with tid3

And I don’t understand how is it possible. We start with the main thread, and create 4 threads: tids[0]... tids[3]. Then, we suspend the execution (by the join instruction): the main thread would wait that tids[0] would stop the execution, tids[0] would wait to tids[1] and so on.

So the output should be:

Hi. I'm thread 0 
Hi. I'm thread 1 
Hi. I'm thread 2 
Hi. I'm thread 3 
Trying to join with tid0 
Trying to join with tid1 
Joined with tid0 
Trying to join with tid2 
Joined with tid1 
Trying to join with tid3 
Joined with tid2 
Joined with tid3

I feel that I don’t understand something really basic. Thanks.

  • 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-04T03:52:32+00:00Added an answer on June 4, 2026 at 3:52 am

    I think what you’re missing is that pthread_create is very different from fork. The created thread starts at the supplied function (printme, in this case) and exits as soon as that function returns. Hence, none of the newly created threads ever reaches the second for loop.

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

Sidebar

Related Questions

I read a book, which give the next example: There is a list, which
Below is the code The Code: #include <stdio.h> int * num(void); int main(void) {
I read a book that write the next program in C, and convet the
I read a book, which gives example of .h file, that equal to interface
In C# in Depth 2nd Edition, Jon Skeet's book - which I've just read
I read a book that uses infix, infixr, and infixl in the sample programs.
I read the book Algorithm Design, chapter 1, it gave a very short description
I want to read a book that systematically introduces dom with live examples. Any
Recently I read a book( CleanCode ) in this book, FUNCTION SHOULD DO ONE
I've read this book section about git branches. I have create a branch called

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.