125k views
3 votes
What is the largest value y can contain after the code runs?

Hint: Since x is relatively small (loop count is small), try writing out the different y values for each thread as they execute. If you want to maximize the value of y, how can two threads 'force' more iterations to happen?
If x was initialized to 2 instead of 3, the largest possible value for y would be 5. Think about how to get this.

User MarGin
by
7.6k points

1 Answer

4 votes

The largest value of y in concurrent programming depends on threads extending a loop's execution through synchronization. Without specific code, the largest y is achieved by thread interleaving that results in additional iterations beyond the initial value of the controlling variable x.

  • The question pertains to a scenario involving concurrent programming where multiple threads are modifying the value of a shared variable y.
  • The aim is to determine the largest possible value y can hold after the execution of a code fragment, given that the initial value of x determines the number of iterations a loop will perform.
  • Not knowing the specific code, it is assumed that threads are competing to increase the value of y within a loop controlled by the variable x.
  • To maximize y, threads would have to be synchronized in a way that they extend the loop's execution as much as possible.
  • In the example provided, if x is initialized to 2, the largest y can become is 5, through potential thread interleaving that result in additional iterations.
  • To generalize a strategy without specific code, when a thread reads the current value of x and then yields (due to a context switch), another thread can decrement x and increase y.
  • If the first thread then resumes, it still believes x is higher than it is and proceeds to modify y.
  • This interleaving can lead to a higher y through extra iterations of the loop.
User Jpnadas
by
7.8k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.

9.4m questions

12.2m answers

Categories