I am looking for an updated read-write lock for win32 with the pthreads rwlock behavior, where read locks can be raised and lowered.
What I want:
pthread_rwlock_rdlock( &lock ); ...read... if( some condition ) { pthread_rwlock_wrlock( &lock ); ...write... pthread_rwlock_unlock( &lock ); } ...read... pthread_rwlock_unlock( &lock );
The update behavior is not required by posix, but it works on linux on mac.
I currently have a working implementation (event-based, semaphore and critical section) that can be updated, but the update may fail when readers are active. If it does not work, you need to read unlock + double check + write lock.
What I have:
lock.rdlock(); ...read... if( some condition ) { if( lock.tryupgrade() ) { ...write... lock.unlock(); return; } else { lock.unlock();
EDIT: Bounty:
I'm still in search, but I want to add some limitations: it is used only inside the process (therefore, based on critical sections it is normal, WIN32 mutexes are not OK), and it should be pure WIN32 API (without MFC, ATL, etc.) . Acquiring read locks should be fast (so getting a read lock should not go into a critical section on its fast path). Is a solution based on InterlockedIncrement possible?
source share