You have two threads that run in parallel, both executing the following code

You have two threads that run in parallel, both executing the following code:


line1: if (counter == 10) return;
line2: ++counter;

where counter is a global variable initially set to 0. If you want to guarantee that both threads will execute return once the variable counter reaches 10, what synchronization strategy will you choose? :



  1.     No synchronization is necessary, both threads will return just fine.
  2.     Acquire a mutex before line1 and release it after line1.
  3.     Acquire a mutex after line1 and release it after line2.
  4.     Acquire a mutex before line1 and release it after line2.