CppCommon  1.0.4.1
C++ Common Library
condition_variable.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_THREADS_CONDITION_VARIABLE_H
10 #define CPPCOMMON_THREADS_CONDITION_VARIABLE_H
11 
13 
14 namespace CppCommon {
15 
17 
33 {
34 public:
39 
42 
44 
51  void NotifyOne();
53 
59  void NotifyAll();
60 
62 
70  void Wait(CriticalSection& cs);
72 
83  template <typename TPredicate>
84  void Wait(CriticalSection& cs, TPredicate predicate);
85 
87 
98  bool TryWaitFor(CriticalSection& cs, const Timespan& timespan);
100 
117  template <typename TPredicate>
118  bool TryWaitFor(CriticalSection& cs, const Timespan& timespan, TPredicate predicate);
119 
121 
131  bool TryWaitUntil(CriticalSection& cs, const UtcTimestamp& timestamp)
132  { return TryWaitFor(cs, timestamp - UtcTimestamp()); }
134 
147  template <typename TPredicate>
148  bool TryWaitUntil(CriticalSection& cs, const UtcTimestamp& timestamp, TPredicate predicate)
149  { return TryWaitFor(cs, timestamp - UtcTimestamp(), predicate); }
150 
151 private:
152  class Impl;
153 
154  Impl& impl() noexcept { return reinterpret_cast<Impl&>(_storage); }
155  const Impl& impl() const noexcept { return reinterpret_cast<Impl const&>(_storage); }
156 
157  static const size_t StorageSize = 48;
158  static const size_t StorageAlign = 8;
159  alignas(StorageAlign) std::byte _storage[StorageSize];
160 };
161 
164 } // namespace CppCommon
165 
166 #include "condition_variable.inl"
167 
168 #endif // CPPCOMMON_THREADS_CONDITION_VARIABLE_H
Condition variable synchronization primitive.
ConditionVariable(ConditionVariable &&cv)=delete
ConditionVariable & operator=(const ConditionVariable &)=delete
ConditionVariable & operator=(ConditionVariable &&cv)=delete
bool TryWaitUntil(CriticalSection &cs, const UtcTimestamp &timestamp)
Try to wait until the given timestamp until condition variable is notified.
bool TryWaitFor(CriticalSection &cs, const Timespan &timespan)
Try to wait for the given timespan until condition variable is notified.
void Wait(CriticalSection &cs)
Wait until condition variable is notified.
void NotifyOne()
Notify one of waiting thread about event occurred.
void NotifyAll()
Notify all waiting threads about event occurred.
bool TryWaitUntil(CriticalSection &cs, const UtcTimestamp &timestamp, TPredicate predicate)
Try to wait until the given timestamp until condition variable is notified using the given predicate.
ConditionVariable(const ConditionVariable &)=delete
Critical section synchronization primitive.
UTC timestamp.
Definition: timestamp.h:248
Condition variable synchronization primitive inline implementation.
Critical section synchronization primitive definition.
C++ Common project definitions.
Definition: token_bucket.h:15