waiter.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <chrono>
19 #include <mutex>
20 
24 class Waiter
25 {
26 public:
27 
28  Waiter() {}
29  ~Waiter() = default;
30 
34  void wait() {
35  std::unique_lock<std::mutex> lck(this->mtx);
36  cv.wait(lck);
37  }
38 
43  void wait_for_millis(std::chrono::milliseconds millis) {
44  std::unique_lock<std::mutex> lck(this->mtx);
45  cv.wait_for(lck, millis);
46  }
47 
52  void wait_for(std::chrono::seconds seconds) {
53  std::unique_lock<std::mutex> lck(this->mtx);
54  cv.wait_for(lck, seconds);
55  }
56 
60  void notify() {
61  std::unique_lock<std::mutex> lck(this->mtx);
62  this->cv.notify_all(); //don't leave anyone blocking
63  }
64 
65 private:
66  std::condition_variable cv;
67  mutable std::mutex mtx;
68  // todo need a count to guard against spurious wakeups
69 };
Waiter()
Definition: waiter.h:28
void notify()
Definition: waiter.h:60
std::condition_variable cv
Definition: waiter.h:66
Definition: waiter.h:24
void wait()
Definition: waiter.h:34
void wait_for(std::chrono::seconds seconds)
Definition: waiter.h:52
~Waiter()=default
std::mutex mtx
Definition: waiter.h:67
void wait_for_millis(std::chrono::milliseconds millis)
Definition: waiter.h:43


dataflow_lite
Author(s): AWS RoboMaker
autogenerated on Fri May 7 2021 02:18:22