Subj : Re: Challenge: Multithreading & Synchronization To : comp.programming.threads From : Uenal Mutlu Date : Sat May 21 2005 02:00 am "Maciej Sobczak" wrote > Uenal Mutlu wrote: > > >>How do you achieve the "wait for something to be done"? > >> > >>Example: > >>One thread moves some elements from one container to another. I want to > >>know (in different thread) when it is done. > > > > I personally do use an atomic flag > > But I mean - how do you *wait* for it to happen? > > If you use the flag, then how do you know *when* to check this flag? > Do you wait? Or do you just check the flag "from time to time"? 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 //... in a non time critical situation I mostly do use something like that: while (!fMyFlag) MySleep(100); // sleeps 100 ms //... As said, in the examples above the variable fMyFlag is an atomic type, because it is accessed by at least 2 threads. But, most people do use WaitForSingleObject() and WaitForMultipleObjects() on Win32. .