Suppose that there are two threads that share a wait-free counter. They would each like to perform fetch, increment and decrement operations, with the following requirements.
Hint. Use two variables to represent the counter. Make sure that one of the variables is only modified by the first thread, and the other variable is only modified by the second thread.
Show a simple solution to the critical section problem using a swap instruction. The solution must work for n threads, for arbitrary n, and must satisfy all of the requirements of the critical section problem, including bounded waiting. The solution should use busy-waiting (spinlock) to wait for access to the critical section. Make your solution as simple as you can. See the solution in the textbook using test-and-set.
Hint: have a shared variable that tells whether any thread is in the critical section, and another variable for each thread telling whether that thread is waiting to get into the critical section.