Boost logo

Threads-Devel :

From: Anthony Williams (anthony_at_[hidden])
Date: 2007-08-03 12:37:44


"Allen Pulsifer" <pulsifer3_at_[hidden]> writes:

>> http://svn.boost.org/svn/boost/branches/thread_rewrite/boost/b
> oost/thread/
>
> Hello Anthony,
>
> Thanks. Glad I asked; I think I'm straight now.
>
> This looks like a standard rwlock implementation: if there is a writer
> waiting, then all new readers block. Correct?

Sort of. A waiting writer will block all new readers until all existing
readers (or the single existing writer) are done. At this point, all waiting
threads (readers and writers) will unblock and compete.

> One comment: I noticed shared_count is only 10 bits. That's probably large
> enough for most applications, but at the same time, small enough that the
> counter could rollover in practice, with catastrophic results.

The 10-bit limit comes from the win32 implementation which needs to count
other things in the same 32-bit value. For pthreads it can easily be increased
to a full unsigned integer without affecting anything, since the atomic
instructions aren't used, so there's no need to cram it into a 32-bit word.

> - test for shared_count reaching its maximum, and then either return busy,
> or make additional readers block until 100 or so slots become free (in this
> situation, there's no reason to wake waiters every time a reader finishes,
> since the machine is probably already overloaded).

Even with an increased limit this is probably wise --- an overflow would be
always be catastrophic, and an increased limit would just make it rarer.

I think for the win32 implementation it's probably wise to rejig the limits,
too. Currently it's 10 bits for the shared stuff and 9 bits for the exclusive
waiters --- I think it's probably safe to limit the waiting writers to 7 bits
(or even only 5) and increase the shared count to compensate. If you've got
multiple waiting writers then you're probably better off with a normal mutex.

> Thanks for the implementation,

You're welcome.

Anthony

-- 
Anthony Williams
Just Software Solutions Ltd - http://www.justsoftwaresolutions.co.uk
Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL

Threads-Devel list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk