Question: This section of code is something that I wrote to provide mutual exclusion between two threads. ( Sorry about the formatting - it's Canvas. )

This section of code is something that I wrote to provide mutual exclusion between two
threads. (Sorry about the formatting - it's Canvas.) Comments?
bool blocked[2];
void thread( int id )
{
int other =(id==0)?1:0;
while ( true)
{
while ( blocked[ other ]) ;// wait in while loop
blocked[ id ]= true;
// critical section here, then...
blocked[ id ]= false;
// other code here...
}
}
As long as blocked[id] is initialized to false (for both) it is fine.
Of course it works, this is really the code for a spin-loop or like a test-and-set.
It does not work. Between falling out of the inside while loop and entering the section, the other
thread could also enter.
 This section of code is something that I wrote to provide

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!