Re: why UI gets hangs



On Mon, 24 Mar 2008 18:47:59 GMT, Dan Bloomquist <public21@xxxxxxxxxxx>
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
.



Relevant Pages

  • Re: Boss Told Me You Cant Use Semaphore, But I Have To Do Something like It!
    ... For example, to create a semaphore, you just call the semaphore create function. ... To create a process-shared mutex, you have to allocate shared memory and then put the mutex in it. ... pthread_mutex_init-- initialization pthread_mutex_lock() -- lock pthread_mutex_unlock-- unlock ...
    (comp.programming.threads)
  • Re: CreateMeteredSection slot?
    ... I need the functional equivalant to a mutex to protect some shared ... they'll wait on the semaphore to be signaled. ... and so skip the ReleaseSemaphore. ... assuming m_state must be in shared memory and the m_waitset semaphore be a ...
    (microsoft.public.win32.programmer.kernel)
  • Re: [PATCH 1/19] MUTEX: Introduce simple mutex implementation
    ... > a mutex was a sensible implementation tradeoff. ... are really counting semaphores, ... >> acceptable patch that introduces a separate data structure. ... > general semaphore, because a mutex has stronger invariants. ...
    (Linux-Kernel)
  • Re: [PATCH 1/19] MUTEX: Introduce simple mutex implementation
    ... >> A counting semaphore is NOT a perfectly fine mutex, ... >> People are indeed unhappy with the naming, ...
    (Linux-Kernel)
  • [patch 00/15] Generic Mutex Subsystem
    ... generic mutex subsystem that we have in the -rt kernel, ... 'simple mutex' code recently posted by David Howells.) ... 'struct mutex' is 16 bytes. ... than the semaphore based kernel, _and_ it also had 2.8 times less CPU ...
    (Linux-Kernel)

Loading