Created
May 22, 2018 12:26
-
-
Save jmarantz/d22b836cee3ca203cc368553eda81ce5 to your computer and use it in GitHub Desktop.
alternate implementation of CondVar using std::condition_variable_any
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CondVar { | |
public: | |
// Note that it is not necessary to be holding an associated mutex to call signal). | |
// See the discussion in | |
// http://en.cppreference.com/w/cpp/thread/condition_variable_any/notify_one | |
// for more details. | |
void notifyOne() { condvar_.notify_one(); } | |
void notifyAll() { condvar_.notify_all(); }; | |
template <class LockGuard> | |
void wait(LockGuard& guard) EXCLUSIVE_LOCKS_REQUIRED(guard) { | |
condvar_.wait(guard); | |
} | |
template <class LockGuard, class Rep, class Period> | |
void waitFor(LockGuard& guard, std::chrono::duration<Rep, Period> duration) | |
EXCLUSIVE_LOCKS_REQUIRED(guard) { | |
condvar_.wait_for(guard, duration); | |
} | |
private: | |
std::condition_variable_any condvar_; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment