Boost logo

Threads-Devel :

From: Russell Kliese (russell_at_[hidden])
Date: 2007-04-11 08:06:18


>> I decided to come up with my own implementation of a timed read write
>> mutex that makes use of the other boost mutex types. I have decided to
>> share this implementation with the boost community in case it might
>> provide inspiration. I'm not an expert on the subject of mutexes, so
>> there may be flaws in my implementation --- I'm happy for any feedback.
>>
>
> Thank you for sharing your implementation. Unfortunately, this also suffers
> from the potential of deadlock. If two readers acquire the lock, and both try
> to upgrade, then they will deadlock.
Thanks for the feedback. I'm not sure this is an issue with the
implementation that I shared because only a single mutex is locked when
upgrading from a reader to a writer. However, your following comment is
correct and moots this implementation.
> Also, if there are multiple readers, it
> is possible that a different thread will try and unlock the "two or more"
> mutex than the one that locked it. This is not necessarily safe. In general,
> mutexes must be unlocked by the same thread that locked them.
>
> I posted a general implementation of a read/write mutex a while back and there
> is a win32-specific implementation which includes upgradeable locks in boost
> CVS on the thread_rewrite branch. I have attached both here for your
> convenience.
>
I've had a quick scan over this code. I'm guessing that the
WaitForSingleObject etc. needs to be replaced by the pthreads equivalents?

At the end of the day, I'm actually after timed locks. Is it strait
forward to alter this code to implement timed locks or does it require
significant rework?

Because write operations are so infrequent for my application, I think
I'm going to resort to a simpler implementation that just makes use of a
while loop with a yield (basically just spin locking) to block the
reader. Unless I can be convinced that it will be relatively simple to
modify your code to work under Linux and to support timed locking :-)

Cheers,

Russell Kliese


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