|
Threads-Devel : |
From: Anthony Williams (anthony_at_[hidden])
Date: 2007-04-11 12:04:00
Russell Kliese <russell_at_[hidden]> writes:
>>> 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?
Yes.
> 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?
It's straight-forward. See attached.
> 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 :-)
Try the attached generic version.
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