The usual pattern for a ReadWriteMutex is to use a semaphore and have the writer loop to acquire all the resources:
inline void write_lock() {
ScopedLock lock(acquire_mutex_);
for (size_t i=0; i < resource_count_; ++i) {
if (sem_wait(semaphore_) < 0) {
fprintf(stderr, "Could not acquire semaphore (%s)\n", strerror(errno));
}
}
}
This is fine except that you have to specify the resource count during semaphore initialization and arbitrarily choosing a resource count of 10 or 99999 does not feel right. Is there a better pattern that would allow "infinite" readers (no need for a resource count) ?