17 std::atomic<bool> _m_stop{
false};
18 std::thread _m_thread;
19 std::function<void()> _m_body;
20 std::function<void()> _m_on_start;
21 std::function<void()> _m_on_stop;
28 while (!this->_m_stop.load()) {
49 explicit
managed_thread(std::function<
void()> body, std::function<
void()> on_start = std::function<
void()>{},
50 std::function<void()> on_stop = std::function<void()>{}) noexcept
51 : _m_body{std::move(body)}
52 , _m_on_start{std::move(on_start)}
53 , _m_on_stop{std::move(on_stop)} { }
59 if (get_state() == state::running) {
62 assert(get_state() == state::stopped || get_state() == state::startable || get_state() == state::nonstartable);
77 bool stopped = _m_stop.load();
79 return state::nonstartable;
80 }
else if (!stopped && !_m_thread.joinable()) {
81 return state::startable;
82 }
else if (!stopped && _m_thread.joinable()) {
83 return state::running;
85 return state::stopped;
87 throw std::logic_error{
"Unknown state"};
95 assert(get_state() == state::startable);
96 _m_thread = std::thread{&managed_thread::thread_main,
this};
97 assert(get_state() == state::running);
104 assert(get_state() == state::running);
107 assert(get_state() == state::stopped);
An object that manages a std::thread; it joins and exits when the object gets destructed.
Definition: managed_thread.hpp:15
~managed_thread() noexcept
Stops a thread, if necessary.
Definition: managed_thread.hpp:58
managed_thread() noexcept=default
Constructs a non-startable thread.
void start()
Moves a managed_thread from startable to running.
Definition: managed_thread.hpp:94
state
Possible states for a managed_thread.
Definition: managed_thread.hpp:67
void stop()
Moves a managed_thread from running to stopped.
Definition: managed_thread.hpp:103
RAC_ERRNO_MSG.
Definition: data_format.hpp:15