I would like to do something like the below for a multi-threaded program:
// wait for variable to become true but don't hog resources
// then re-sync queues
Is something like this a good solution?
while (!ready) {
Thread.Sleep(250); // pause for 1/4 second;
};
The code above will work, and maybe appropriate in some circumstances.
You could also look at a critical section or semaphore - this will make your application block and wait until the resource becomes available,
Your thread that does the work grabs the mutex, does some work, meanwhile, the main method also tries to grab the same mutex, but can't. when the worker thread(s) exit, they release the mutex and your main thread can pass the critical section and continue.
First of all, you need to declare your 'ready' variable at least 'volatile' or this could have nasty side effects. Secondly, sleeping that long vefore reevaluating the condition is only a good idea if the duration it might take is indeed very long, let's say a few minutes.
Using the WinAPI's Event functions (
CreateEvent, SetEvent(), WaitForSingleEvent()
) is the best way to do it. Of course it introduces some overhead, but usually it's fine.If you want to stick with your solution, looping and rechecking the condition a few times before you sleep again could improve performance in some scenarios.
Try to use Event (kernel object) instead of simple variable and replace your loop by:
Here's how you do it using boost:
For the sake of brevity, I didn't include the thread spawning code.
The
boost::lock_guard
uses the RAII idiom to automatically unlock the mutex when the lock object goes out of scope. Very useful for preventing deadlocks in case of exceptions.I find condition variables less error prone than Microsoft's Event objects. If you use boost.Thread, you'll have the added benefit of cross-platform potability.
No, this is not a good solution. First it might sleep too long. Second it's easy for threads to get into lockstep. Here's couple of links to MSDN articles on proper synchronization techniques:
The raw Win32 API has EVENT for doing this, here's a usage example:
http://msdn.microsoft.com/en-us/library/ms686915(VS.85).aspx
However, that API is C-oriented and particular to Windows. If writing a C++ program you might consider making your code more platform independent by using something like boost::threads, which has an analogue in Conditions.
A caveat I've found is that Windows can WaitForMultipleObjects, thus waiting on several events (and other handle classes) at a time. boost has no parallel AFAIK.