Question: What is wrong with this code? In addition to the line of code, why exactly is it a problem? How would two threads incorrectly execute

What is wrong with this code? In addition to the line of code, why exactly is it a problem? How would two threads incorrectly execute that line, down to the pseudo-assembly level? #include #include

void *dowork(void* arg) {

int x;

int* a = (int*)arg;

for(x = 0; x < 100000; x++) {

(*a)++;

}

pthread_exit(0);

} void main(int argc, char** argv) {

int i;

pthread_t p[2];

int x;

i = 0;

for(x = 0; x < 2; x++) {

pthread_create(&p[x], NULL, dowork, &i);

}

for(x = 0; x < 2; x++) {

pthread_join(p[x], NULL);

}

printf("The final value of i is %d ", i);

pthread_exit(0);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!