Boost logo

Boost :

From: Anthony Williams (anthony_w.geo_at_[hidden])
Date: 2008-04-21 10:03:59

"Peter Dimov" <pdimov_at_[hidden]> writes:

> Anthony Williams:
>> "Peter Dimov" <pdimov_at_[hidden]> writes:
>>> I'm seeing a deadlock on a dual core Pentium D from
>>> libs/smart_ptr/test/sp_atomic_mt_test.cpp when USE_RWLOCK is defined
>>> (which
>>> causes it to use boost::shared_mutex). It works on uniprocessor.
>> How often, on what platform?
> Every time, on a dual core Pentium D running XP64. The program is compiled
> as 32 bit with MSVC 7.1. It looks like a livelock rather than a deadlock,
> since the program keeps spinning its wheels at 100% but never completes. The
> writer thread is probably either blocked or starved. This happens on another
> (quad core) machine too for which I don't know the specs at the moment.

Here's the thing: your writer is running in a tight loop, and so are your
readers, with each reacquiring the lock immediately after releasing
it. shared_mutex is not designed for this scenario, since you have high
contention. shared_mutex is designed for infrequent updates. Your test should
complete eventually (it takes 2-5 minutes on my machine), but it is likely
that the problem is indeed due to writer starvation.

In the current implementation, a blocked writer will prevent further readers
acquiring the lock, thus if you frequently try and obtain a writer lock this
reduces the potential reader concurrency. However, in order to avoid reader
starvation, the writer is not automatically granted the lock when the read
lock is released: instead it has to compete for the privilege with the blocked
readers. It is up to the OS how it chooses to schedule it.

Amusingly, the way I've programmed the upgrade_lock means that your test runs
quicker (22 seconds vs 2-5 minutes) if you do:

        boost::upgrade_lock<boost::shared_mutex> up_lock( rw );
        boost::unique_lock<boost::shared_mutex> lock( up_lock.move() );

rather than

        boost::unique_lock<boost::shared_mutex> lock( rw );


Anthony Williams            | Just Software Solutions Ltd
Custom Software Development |
Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL

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