CppCommon  1.0.4.1
C++ Common Library
latch.inl
Go to the documentation of this file.
1 
9 namespace CppCommon {
10 
11 inline Latch::Latch(int threads) noexcept : _generation(0), _threads(threads)
12 {
13  assert((threads > 0) && "Latch threads counter must be greater than zero!");
14 }
15 
16 inline bool Latch::TryWaitFor(const Timespan& timespan) noexcept
17 {
18  std::unique_lock<std::mutex> lock(_mutex);
19 
20  // Check the latch threads counter value
21  if (_threads == 0)
22  return true;
23 
24  // Remember the current latch generation
25  int generation = _generation;
26 
27  // Wait for the next latch generation
28  return _cond.wait_for(lock, timespan.chrono(), [&, this]() { return generation != _generation; });
29 }
30 
31 inline bool Latch::TryWaitUntil(const Timestamp& timestamp) noexcept
32 {
33  std::unique_lock<std::mutex> lock(_mutex);
34 
35  // Check the latch threads counter value
36  if (_threads == 0)
37  return true;
38 
39  // Remember the current latch generation
40  int generation = _generation;
41 
42  // Wait for the next latch generation
43  return _cond.wait_until(lock, timestamp.chrono(), [&, this]() { return generation != _generation; });
44 }
45 
46 } // namespace CppCommon
bool TryWaitUntil(const Timestamp &timestamp) noexcept
Try to wait for the latch until the given timestamp.
Definition: latch.inl:31
Latch(int threads) noexcept
Default class constructor.
Definition: latch.inl:11
bool TryWaitFor(const Timespan &timespan) noexcept
Try to wait for the latch for the given timespan.
Definition: latch.inl:16
C++ Common project definitions.
Definition: token_bucket.h:15