DUNE-DAQ
DUNE Trigger and Data Acquisition software
Toggle main menu visibility
Loading...
Searching...
No Matches
dunedaq
sourcecode
utilities
src
WorkerThread.cpp
Go to the documentation of this file.
1
13
14
#include "
utilities/WorkerThread.hpp
"
15
#include "
utilities/Issues.hpp
"
16
17
#include <memory>
18
#include <string>
19
20
dunedaq::utilities::WorkerThread::WorkerThread
(std::function<
void
(std::atomic<bool>&)> do_work)
21
:
m_thread_running
(false)
22
,
m_working_thread
(nullptr)
23
,
m_do_work
(do_work)
24
{
25
}
26
27
void
28
dunedaq::utilities::WorkerThread::start_working_thread
(
const
std::string& name)
29
{
30
if
(
thread_running
()) {
31
throw
ThreadingIssue(
ERS_HERE
,
32
"Attempted to start working thread "
33
"when it is already running!"
);
34
}
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());
39
if
(rc != 0) {
40
std::ostringstream s;
41
s <<
"The name "
<< name <<
" provided for the thread is too long."
;
42
ers::warning
(ThreadingIssue(
ERS_HERE
, s.str()));
43
}
44
}
45
46
void
47
dunedaq::utilities::WorkerThread::stop_working_thread
()
48
{
49
if
(!
thread_running
()) {
50
throw
ThreadingIssue(
ERS_HERE
,
51
"Attempted to stop working thread "
52
"when it is not running!"
);
53
}
54
m_thread_running
=
false
;
55
56
if
(
m_working_thread
->joinable()) {
57
try
{
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());
61
}
62
}
else
{
63
throw
ThreadingIssue(
ERS_HERE
,
"Thread not in joinable state during working thread stop!"
);
64
}
65
}
ERS_HERE
#define ERS_HERE
Definition
LocalContext.hpp:141
WorkerThread.hpp
dunedaq::utilities::WorkerThread::m_working_thread
std::unique_ptr< std::jthread > m_working_thread
Definition
WorkerThread.hpp:100
dunedaq::utilities::WorkerThread::stop_working_thread
void stop_working_thread()
Stop the working thread.
Definition
WorkerThread.cpp:47
dunedaq::utilities::WorkerThread::start_working_thread
void start_working_thread(const std::string &name="noname")
Start the working thread (which executes the do_work() function).
Definition
WorkerThread.cpp:28
dunedaq::utilities::WorkerThread::m_thread_running
std::atomic< bool > m_thread_running
Definition
WorkerThread.hpp:99
dunedaq::utilities::WorkerThread::thread_running
bool thread_running() const
Determine if the thread is currently running.
Definition
WorkerThread.hpp:91
dunedaq::utilities::WorkerThread::m_do_work
std::function< void(std::atomic< bool > &)> m_do_work
Definition
WorkerThread.hpp:101
dunedaq::utilities::WorkerThread::WorkerThread
WorkerThread(std::function< void(std::atomic< bool > &)> do_work)
WorkerThread Constructor.
Definition
WorkerThread.cpp:20
ers::warning
void warning(const Issue &issue)
Definition
ers.hpp:126
Issues.hpp
Generated on
for DUNE-DAQ by
1.17.0