Skip to content

Instantly share code, notes, and snippets.

@rightfold
Last active November 27, 2015 23:32
Show Gist options
  • Save rightfold/e23bc9ada79afdf2d5da to your computer and use it in GitHub Desktop.
Save rightfold/e23bc9ada79afdf2d5da to your computer and use it in GitHub Desktop.
Just like a shared mutex, except acquiring a shared lock while a thread is waiting to acquire a unique lock will block.
template<typename SharedMutex>
class unique_lock_prioritizing_shared_mutex {
public:
void lock() {
std::lock_guard<decltype(control)> lock(control);
mutex.lock();
}
void unlock() {
mutex.unlock();
}
bool try_lock() {
return mutex.try_lock();
}
void lock_shared() {
std::lock_guard<decltype(control)> lock(control);
mutex.lock_shared();
}
bool try_lock_shared() {
return mutex.try_lock_shared();
}
void unlock_shared() {
mutex.unlock_shared();
}
private:
std::mutex control;
SharedMutex mutex;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment