Re: why UI gets hangs
On Mon, 24 Mar 2008 18:47:59 GMT, Dan Bloomquist <public21@lakeweb.com>
wrote:
My objective was an example of 'not' blocking the thread and keeping the
GUI updated. In that light, do you see a problem with the way I use the
semaphore strct.bTReady?
****
It isn't a semaphore; it is a bool variable.
The wiki author calls it a 'binary semaphore', better known as a mutex.
http://en.wikipedia.org/wiki/Semaphore_(programming)
I would think any object that is used as a signal between threads would
qualify as a semaphore.
It is a bool variable, and as such, it's just dumb data. It's neither a
mutex nor a semaphore, data types which provide signal and wait operations
useful for multithreaded programming. For example, consider the mutex
operations:
1. Wait: This operation acquires the mutex if it is signaled, such that no
other thread can acquire it at the same time. If the mutex isn't available,
the thread enters an efficient wait state and blocks until the mutex
becomes signaled.
2. Signal: This operation releases the mutex so that it may be acquired
again by the Wait operation.
Clearly, bool is nothing like this. You're using strct.bTReady sort of like
an event, which you poll during your loop. Joe's comment about it being
unsafe on a multiprocessor has to do with memory visibility; even though
Thread A sets a bool to true, and Thread B observes it, the two threads
don't necessarily share a completely consistent view of memory. For
multiprocessor systems that require memory barriers (e.g. Itanium), you
will have to use a type or operations that provide the necessary barriers.
All the Windows synchronization objects provide them as necessary.
--
Doug Harrison
Visual C++ MVP