Thread and CriticalSections

Started by
10 comments, last by hellfire 21 years, 8 months ago
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.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
DrPizza & developer, you''re both right about critical sections. The conceptual difference between a mutex and a critical section is what I base my choice on though.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

This topic is closed to new replies.

Advertisement