previous | start | next

Busy Waiting and Critical Sections

First, make a minor change. Rename the shared variable as turn.

The idea would be (if it worked) that when turn is 0, thread 1 would wait.

When turn is 1, thread 0 would wait.

The code for the two threads would be modified to test turn like this:

Thread 0 Thread 1
0)   for(i = 0; i < N; i++) {
1)     while ( turn != 0 )      
        {                       
        }                       
2)     critical section        
3)     turn = 1;               

4)     remainder section       
     }
5)                              
0)     for(j = 0; j < M; j++) {
1)       while ( turn != 1 )    
          {                     
          }                     
2)       critical section      
3)       turn = 0;             

4)       remainder section     
       }
5)                            

Although this would solve the synchronization problem where the two threads must alternate, it would not solve the critical section problem as it would violate the progress requirement.



previous | start | next