1 #ifndef TERMOX_COMMON_LISTENER_THREAD_HPP
2 #define TERMOX_COMMON_LISTENER_THREAD_HPP
4 #include <condition_variable>
15 template <
typename Function_t>
22 : action_{std::forward<Function_t>(action)}
36 assert(!fut_.valid());
37 fut_ = std::async(std::launch::async, [
this] {
40 auto lock = std::unique_lock{mtx_};
42 cv_.wait(lock, [
this] {
return notify_ || exit_; });
59 auto const lock = std::lock_guard{mtx_};
69 auto const lock = std::lock_guard{mtx_};
88 std::condition_variable cv_;
89 std::future<void> fut_;
Thread that waits to be notified, then performs some action.
Definition: listener_thread.hpp:16
void notify()
Wakes up the thread and has it run the action once.
Definition: listener_thread.hpp:56
~Listener_thread()
Shutdown the listener thread.
Definition: listener_thread.hpp:26
void exit()
Notify the thread to exit.
Definition: listener_thread.hpp:66
void wait()
Blocks until the thread is dead.
Definition: listener_thread.hpp:76
Listener_thread(Function_t &&action)
Launch the listener thread.
Definition: listener_thread.hpp:21
void launch()
Start the thread that will wait for notification.
Definition: listener_thread.hpp:34