179k views
2 votes
Q3: We run the following code on two threads:

// Hint: The following variables are declared outside the #pragma directive
/ therefore, these variables are shared for all threads.
int y = 0;
int x = 3;
#pragma omp parallel
{
while(x > 0) {
y = y + 1;
X = X - 1:
?
Note that the expression y = y + 1 is equivalent to three instructions: load value of y, add 1, store result to y. Another thread can execute in between those instructions. Moreover, y is a shared variable, which means that any changes that one thread does to y will be seen by the other thread. The same can be said for variable Ã. Exploit this fact when answering the questions below. It might be helpful if you can answer Q2 first before answering this
one since you can imagine breaking down the C code into the assembly for a more fine-grained analysis.
What is the smallest value y can contain after the code runs?

User Mbrannig
by
8.0k points

1 Answer

1 vote

Final answer:

The smallest value y can contain after the code runs is 3.

Step-by-step explanation:

The code provided runs on two threads and updates the shared variable y by incrementing it by 1 in a loop while decreasing x by 1. Since the variable y is updated by multiple threads, the final value of y can vary depending on the interleaving of the thread execution. However, the smallest value y can contain after the code runs is 3.

User Bronx
by
8.2k points