CppCommon  1.0.4.1
C++ Common Library
named_condition_variable.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_THREADS_NAMED_CONDITION_VARIABLE_H
10 #define CPPCOMMON_THREADS_NAMED_CONDITION_VARIABLE_H
11 
12 #include "time/timestamp.h"
13 
14 #include <memory>
15 #include <string>
16 
17 namespace CppCommon {
18 
20 
27 {
28 public:
30 
33  explicit NamedConditionVariable(const std::string& name);
37 
40 
42  const std::string& name() const;
43 
45 
52  void NotifyOne();
54 
60  void NotifyAll();
61 
63 
68  void Wait();
70 
80  template <typename TPredicate>
81  void Wait(TPredicate predicate);
82 
84 
93  bool TryWaitFor(const Timespan& timespan);
95 
111  template <typename TPredicate>
112  bool TryWaitFor(const Timespan& timespan, TPredicate predicate);
113 
115 
124  bool TryWaitUntil(const UtcTimestamp& timestamp)
125  { return TryWaitFor(timestamp - UtcTimestamp()); }
127 
139  template <typename TPredicate>
140  bool TryWaitUntil(const UtcTimestamp& timestamp, TPredicate predicate)
141  { return TryWaitFor(timestamp - UtcTimestamp(), predicate); }
142 
143 private:
144  class Impl;
145 
146  Impl& impl() noexcept { return reinterpret_cast<Impl&>(_storage); }
147  const Impl& impl() const noexcept { return reinterpret_cast<Impl const&>(_storage); }
148 
149  static const size_t StorageSize = 168;
150  static const size_t StorageAlign = 8;
151  alignas(StorageAlign) std::byte _storage[StorageSize];
152 };
153 
156 } // namespace CppCommon
157 
159 
160 #endif // CPPCOMMON_THREADS_NAMED_CONDITION_VARIABLE_H
Named condition variable synchronization primitive.
bool TryWaitUntil(const UtcTimestamp &timestamp, TPredicate predicate)
Try to wait until the given timestamp until condition variable is notified using the given predicate.
void NotifyOne()
Notify one of waiting thread about event occurred.
NamedConditionVariable(const NamedConditionVariable &)=delete
bool TryWaitUntil(const UtcTimestamp &timestamp)
Try to wait until the given timestamp until condition variable is notified.
void NotifyAll()
Notify all waiting threads about event occurred.
NamedConditionVariable(const std::string &name)
Default class constructor.
const std::string & name() const
Get the condition variable name.
void Wait()
Wait until condition variable is notified.
bool TryWaitFor(const Timespan &timespan)
Try to wait for the given timespan until condition variable is notified.
NamedConditionVariable & operator=(const NamedConditionVariable &)=delete
NamedConditionVariable(NamedConditionVariable &&cv)=delete
NamedConditionVariable & operator=(NamedConditionVariable &&cv)=delete
UTC timestamp.
Definition: timestamp.h:248
Named condition variable synchronization primitive inline implementation.
C++ Common project definitions.
Definition: token_bucket.h:15
Timestamp definition.