BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
wakeup_signal.hpp
1#ifndef BEHAVIORTREECORE_WAKEUP_SIGNAL_HPP
2#define BEHAVIORTREECORE_WAKEUP_SIGNAL_HPP
3
4#include <atomic>
5#include <chrono>
6#include <condition_variable>
7#include <mutex>
8
9namespace BT
10{
11
12class WakeUpSignal
13{
14public:
15 /// Return true if the timeout was NOT reached and the
16 /// signal was received.
17 bool waitFor(std::chrono::microseconds usec)
18 {
19 std::unique_lock<std::mutex> lk(mutex_);
20 auto res = cv_.wait_for(lk, usec, [this] { return ready_.load(); });
21 ready_ = false;
22 return res;
23 }
24
25 void emitSignal()
26 {
27 ready_ = true;
28 cv_.notify_all();
29 }
30
31private:
32 std::mutex mutex_;
33 std::condition_variable cv_;
34 std::atomic_bool ready_ = false;
35};
36
37} // namespace BT
38
39#endif // BEHAVIORTREECORE_WAKEUP_SIGNAL_HPP
Definition: wakeup_signal.hpp:13
bool waitFor(std::chrono::microseconds usec)
Definition: wakeup_signal.hpp:17
Definition: action_node.h:24