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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:22:04+00:00 2026-06-11T03:22:04+00:00

I write a code to print out strings: "Thread 0" to "Thread 4" using

  • 0

I write a code to print out strings: "Thread 0" to "Thread 4" using pthread.

Here is my code:

Case 1:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *print_message_function(void* parameter) {
    long *i = (long *)parameter;
    printf("Thread %ld\n", *i);
    pthread_exit(0);
}

int main(int argc, char *argv[]) {
    pthread_t threads[5];
    long i = 0;
    for (i = 0; i < 5; i++) {
        pthread_create(&threads[i], 0, print_message_function, (void *)&i);
    }
    
    pthread_exit(NULL);
}

But the result is:

Thread 2
Thread 3
Thread 3
Thread 4
Thread 5

or:

Thread 0
Thread 0
Thread 0
Thread 0
Thread 0

It changed when I run it again. So I don’t know why the values I passed are (2 to 5) or all (0) or ….. (many cases). I think my arguments I passed is from 0 to 4.

Case 2:

When I change to the new code:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *print_message_function(void *parameter);

int main(int argc, char *argv[]) {
    pthread_t threads[5];
    int i = 0;
    for (i = 0; i < 5; i++) {
        char *msg = (char*)malloc(sizeof(char));
        sprintf(msg, "Thread %d", i);
        pthread_create(&threads[i], 0, print_message_function, (void *)msg);
    }
}

void *print_message_function(void *parameter) {
    printf("%s\n", (char *)parameter);
}

The result is:

Thread 1
Thread 0
Thread 3
Thread 2
Thread 4
Thread 4

It means the loop run 6 times! Why?

  • 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-11T03:22:06+00:00Added an answer on June 11, 2026 at 3:22 am

    Change Case 1 to this:

    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    void *print_message_function(void* parameter) {
        long i = (long)parameter;  // <<<
        printf("Thread %ld\n", i); // <<<
        pthread_exit(0);
    }
    
    int main(int argc, char *argv[]) {
        pthread_t threads[5];
        long i = 0;
        for (i = 0; i < 5; i++) {
            pthread_create(&threads[i], 0, print_message_function, (void *)i); // <<<
        }
    
        pthread_exit(NULL);
    }
    

    The reason that you were seeing inconsistent results before was because you were passing a pointer to each thread where each pointer was pointing at the same local variable, which you were then modifying.

    In Case 2 you are mallocing only a single char and then trying to write a string to it. It should be fairly easy to fix.

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

Sidebar

Related Questions

I am trying to write some code to print out a square made up
I'm trying to write a code that could print something like this -xv -xvv
So far I have managed to write some code that should print the source
I am trying to write a code that compares two strings and return the
I'm trying to write code that can print only filled text boxes in the
I wrote a simple code to capture the netdevice notifications and simply print their
I'm trying to write code in my controller that when run, will create a
I m trying write code that after reset set up rrpmax as 3000. It
I am trying to write code for getting my cords on the mobile. I
How do I write code to connect through Windows VPN client? I want this

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.