
6 Feb
2011
6 Feb
'11
11:10 p.m.
Howard Hinnant wrote: ...
int main() {
ting::upgrade_mutex m; ting::upgrade_lock<ting::upgrade_mutex> readlock(m); // reads go here
The problem is that only one thread can do this at a time, and I suspect that in the original problems, there are many such readers, some of which need to then write, but only occasionally. This is one way to do it: take read lock; int v = data.version_; // reads go here drop read lock; if( need to write ) { take write lock; if( v != data.version_ ) { retry reads; } if( still need to write ) { ++data.version_; // writes go here } drop write lock; } Note that no upgrade locks are needed.