DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
WorkerThread.cpp
Go to the documentation of this file.
1
17
18dunedaq::utilities::WorkerThread::WorkerThread(std::function<void(std::atomic<bool>&)> do_work)
19 : m_thread_running(false)
20 , m_working_thread(nullptr)
21 , m_do_work(do_work)
22{}
23
24void
25dunedaq::utilities::WorkerThread::start_working_thread(const std::string& name)
26{
27 if (thread_running()) {
28 throw ThreadingIssue(ERS_HERE,
29 "Attempted to start working thread "
30 "when it is already running!");
31 }
32 m_thread_running = true;
33 m_working_thread.reset(new std::thread([&] { m_do_work(std::ref(m_thread_running)); }));
34 auto handle = m_working_thread->native_handle();
35 auto rc = pthread_setname_np(handle, name.c_str());
36 if (rc != 0) {
37 std::ostringstream s;
38 s << "The name " << name << " provided for the thread is too long.";
39 ers::warning(ThreadingIssue(ERS_HERE, s.str()));
40 }
41}
42
43void
44dunedaq::utilities::WorkerThread::stop_working_thread()
45{
46 if (!thread_running()) {
47 throw ThreadingIssue(ERS_HERE,
48 "Attempted to stop working thread "
49 "when it is not running!");
50 }
51 m_thread_running = false;
52
53 if (m_working_thread->joinable()) {
54 try {
55 m_working_thread->join();
56 } catch (std::system_error const& e) {
57 throw ThreadingIssue(ERS_HERE, std::string("Error while joining thread, ") + e.what());
58 }
59 } else {
60 throw ThreadingIssue(ERS_HERE, "Thread not in joinable state during working thread stop!");
61 }
62}
#define ERS_HERE
void warning(const Issue &issue)
Definition ers.hpp:115