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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:32:29+00:00 2026-05-30T15:32:29+00:00

I was practicing some multithreaded programs, but I could not figure the logic behind

  • 0

I was practicing some multithreaded programs, but I could not figure the logic behind this output.

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

int print_message(void* ptr);

int main()
{
pthread_t thread1,thread2;
char *mesg1 = "Thread 1";
char *mesg2 = "Thread 2";

int iret1, iret2;

pthread_create(&thread1, NULL, print_message, (void *)mesg1);
pthread_create(&thread2, NULL, print_message, (void *)mesg2);

pthread_join(thread1,(void*)&iret1 );
pthread_join(thread2, (void*)&iret2);

printf("Thread 1 return : %d\n", (int)iret1);
printf("Thread 2 return : %d\n", (int)iret2);
return 0;

}

int print_message(void *ptr)
 {
char *mesg;
static int i=0;
mesg = (char *)ptr;
printf("%s\n",mesg);
i++;
return ((void*)i);

}

I was expecting the output

Thread 1
Thread 2
Thread 1 return : 1
Thread 2 return : 2

but I am getting the output

Thread 1
Thread 2
Thread 1 return : 0
Thread 2 return : 2

Could some please clarify this to me ? And please point if any errors in usage of pthread functions.

  • 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-30T15:32:31+00:00Added an answer on May 30, 2026 at 3:32 pm

    The variable i is shared between both threads because it is static. The behaviour of modifying a variable between multiple threads is undefined, so, in fact, both the output you get and the output you want to get are “wrong” in the sense that the compiler is under no obligation to give it to you. In fact, I was able to get the output to change depending on the optimisation level I used and it will undoubtedly be different based on the platform.

    If you want to modify i, you should use a mutex:

    int print_message(void *ptr)
    {
      static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
      char *mesg;
      static int i=0;
      int local_i;
    
      mesg = (char *)ptr;
      printf("%s\n",mesg);
      if (pthread_mutex_lock(&mutex) == 0) {
        local_i = ++i;
        pthread_mutex_unlock(&mutex);
      }
      return ((void*)local_i);
    }
    

    If you do not use a mutex, you will never be sure to get the output you think you should get.

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

Sidebar

Related Questions

I've been practicing some animation on Android but I could not get my code
I'm practicing some XSL and using this XML document as a simple example: <?xml
Firstly this is not a homework question. I am practicing my knowledge on java.
I am practicing with PHP and AJAX but I have some problems! I'm trying
Actually i had been practicing some of the java programs today(trying to learn it
I'm practicing object oriented syntax in javascript and am having some problems. This is
I've never done OpenGL, but I'm looking for some pointers on this particular question
I have been practicing TDD and (some) XP for a few years now and
I'm practicing Jquery and I've written this simple Jquery statement: var someText = $(table
Whilst practicing some TDD at work for an ASP.Net MVC project, I ran into

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.