My questions are :How many values can the output of the following code take? What are these values? What is the reason for different values? How can we prevent different values from occurring?
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<unistd.h>
int shared_data = 100;
void* thread_function(void *args)
{
long temp;
long *j = (long *) args;
temp = shared_data;
temp = temp + &j;
shared_data = temp;
return (NULL);
}
int main()
{
long i = 0;
long tinfo[] = {1,2,3};
pthread_t ids[3];
for(int i=0 ;i<3;i++)
{
pthread_create(ids+i,NULL,&thread_function, &tinfo[i]);
}
for(int i=0 ;i<3;i++)
{
pthread_join(ids[i],NULL);
}
printf("shared data = %dn",shared_data);
}
I was working on system programming and operating system and saw this exercise. I have tried to make a research but the topic is very new to me. It is not an homework or exam question, It is one of the past interview question of an institution.