Subj : Re: Challenge: Multithreading & Synchronization To : comp.programming.threads From : David Schwartz Date : Fri May 20 2005 04:58 pm "Uenal Mutlu" <520001085531-0001@t-online.de> wrote in message news:d6lprf$70p$01$1@news.t-online.com... > It depends on the situation. > In a time-critical application I usually do the following: > while (!fMyFlag) > MyYield(); // gives up the rest of the time slice, ie. > SwitchToOtherThread > //... And if there is no other thread? > in a non time critical situation I mostly do use something like that: > while (!fMyFlag) > MySleep(100); // sleeps 100 ms > //... Hard to imagine a mutex in a non-time critical situation. Why deliberately write bad code? > As said, in the examples above the variable fMyFlag is an atomic type, > because > it is accessed by at least 2 threads. The use of an atomic type is insufficient. For platform-specific code, memory barriers or interlocked operations must be used. For portable code, mutexes must be used. Atomic operations are atomic with respect to a single processor only, that is, code cannot be interrupted or signalled during the operation. They have no special SMP or multi-threading semantics. DS .