
Anthony Williams wrote:
"Peter Dimov" <pdimov@mmltd.net> writes:
I have produced a read/write mutex implementation as a side effect, available at
http://www.pdimov.com/cpp/rw_mutex.cpp
that is lock-free when there is no contention (i.e. when a reader attempts a lock and no writers are active, or when a writer attempts a lock and the lock is free.)
It appears to work (famous last words.) If you see any subtle problems or can stress test it on a multiprocessor, please let me know.
This implementation either starves writers or starves readers. Running your test code lets the writers all do some work, then the readers take over until they're all finished, then the writers alternate until they're all done.
Contented readers and all writers compete on the same mutex (mx_); I believe that this makes the implementation as fair as an ordinary mutex. If you run my test with all yields removed, you'll see that all threads finish more or less at the same time, approximately in the order in which they were started.