21 : m_thread_running(false)
22 , m_working_thread(nullptr)
30 if (thread_running()) {
32 "Attempted to start working thread "
33 "when it is already running!");
35 m_thread_running =
true;
36 m_working_thread = std::make_unique<std::jthread>([&] { m_do_work(std::ref(m_thread_running)); });
37 auto handle = m_working_thread->native_handle();
38 auto rc = pthread_setname_np(handle, name.c_str());
41 s <<
"The name " << name <<
" provided for the thread is too long.";
49 if (!thread_running()) {
51 "Attempted to stop working thread "
52 "when it is not running!");
54 m_thread_running =
false;
56 if (m_working_thread->joinable()) {
58 m_working_thread->join();
59 }
catch (std::system_error
const& e) {
60 throw ThreadingIssue(
ERS_HERE, std::string(
"Error while joining thread, ") + e.what());
63 throw ThreadingIssue(
ERS_HERE,
"Thread not in joinable state during working thread stop!");
void stop_working_thread()
Stop the working thread.
void start_working_thread(const std::string &name="noname")
Start the working thread (which executes the do_work() function)
WorkerThread(std::function< void(std::atomic< bool > &)> do_work)
WorkerThread Constructor.
void warning(const Issue &issue)