Boost logo

Boost :

From: E. Gladyshev (egladysh_at_[hidden])
Date: 2003-10-20 15:53:36

> Daniel Spangenberg <dsp_at_[hidden]> writes:
> Question is: Can I use the current thread library to realize a
> read/write mutex (accepting
> multiple readers from different threads but prevents writer from
> access)
> on my own?
> If so, how should I do this? If this would help to give a more
> precise answer: My
> target platform is Win32, although a generic solution would be
> wunderful, of course ;-))
> (More worse: It must be a mutex accessible/protectable from threads
> of different
> processes...).


If you are working on an industrial mission critical control application
adn thinking about using boost::mutex,
I'd suggest to take a look on how boost::mutex is actually implemented
on Win32 platform.
As far as I remember, it doesn't handle the WAIT_ABANDONED condition
correctly if at all. Here is how it was in boost v1.30

void try_mutex::do_lock()
    int res = 0;
    res = WaitForSingleObject(reinterpret_cast<HANDLE>(m_mutex), INFINITE);
    assert(res == WAIT_OBJECT_0);

bool try_mutex::do_trylock()
    unsigned int res = 0;
    res = WaitForSingleObject(reinterpret_cast<HANDLE>(m_mutex), 0);
    assert(res != WAIT_FAILED && res != WAIT_ABANDONED);
    return res == WAIT_OBJECT_0;

If in try_mutex::do_lock(), the 'assert' statement is a debug
version only macro, the try_mutex::do_lock() method is basically
ignoring the WaitForSingleObject() return code completely.
I don't think that this implementation is acceptable for
a serious multi-thread application.

Also, in most critical (especially cross-process) applications WAIT_ABANDONED
is a "normal" condition that must be dealt with.

If you are interested, we can discuss this condition
in more details. It is not as straight forward as it might seem.


Do you Yahoo!?
The New Yahoo! Shopping - with improved product search

Boost list run by bdawes at, gregod at, cpdaniel at, john at