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? :
- No synchronization is necessary, both threads will return just fine.
- Acquire a mutex before line1 and release it after line1.
- Acquire a mutex after line1 and release it after line2.
- Acquire a mutex before line1 and release it after line2.