Skip to content

Instantly share code, notes, and snippets.

@jmarantz
Created May 22, 2018 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmarantz/d22b836cee3ca203cc368553eda81ce5 to your computer and use it in GitHub Desktop.
Save jmarantz/d22b836cee3ca203cc368553eda81ce5 to your computer and use it in GitHub Desktop.
alternate implementation of CondVar using std::condition_variable_any
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