quote:Original post by Bashar
// ** bolt is a global boolean, which is seen by both threads
if( bolt )
{
bolt = false;
// ** do your work
bolt = true;
}
This is NOT guaranteed to function correctly - the bolt variable constitutes a RMW sequence and needs synchronization itself to be multi-thread safe.
After the ''if(bolt)'' check the CPU may be swapped to the other thread which may execute the same line (both will succeed), and both will be executing code inside the critical section.
(P.S. SpinLock''s are only useful on multi-processor machines - they just waste CPU time on single systems)
In computer science terms, the code between the mutex locking/unlocking is called a critical section. In Win32 terms, a critical section is a process specific mutex.