DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
dunedaq::utilities::WorkerThread Class Reference

WorkerThread contains a thread which runs the do_work() function. More...

#include <WorkerThread.hpp>

Public Member Functions

 WorkerThread (std::function< void(std::atomic< bool > &)> do_work)
 WorkerThread Constructor.
 
void start_working_thread (const std::string &name="noname")
 Start the working thread (which executes the do_work() function)
 
void stop_working_thread ()
 Stop the working thread.
 
bool thread_running () const
 Determine if the thread is currently running.
 
 WorkerThread (const WorkerThread &)=delete
 WorkerThread is not copy-constructible.
 
WorkerThreadoperator= (const WorkerThread &)=delete
 WorkerThread is not copy-assginable.
 
 WorkerThread (WorkerThread &&)=delete
 WorkerThread is not move-constructible.
 
WorkerThreadoperator= (WorkerThread &&)=delete
 WorkerThread is not move-assignable.
 

Private Attributes

std::atomic< boolm_thread_running
 
std::unique_ptr< std::jthread > m_working_thread
 
std::function< void(std::atomic< bool > &)> m_do_work
 

Detailed Description

WorkerThread contains a thread which runs the do_work() function.

WorkerThread runs a given function in a std::jthread and allows that work function to be started and stopped via the start_working_thread() and stop_working_thread() methods, respectively. The work function takes a std::atomic<bool>& which indicates whether the thread should continue running, so a typical implementation of a work function is:

void do_work(std::atomic<bool>& running_flag){
while(running_flag.load()){
// do something ...
}
}

If your do_work function is a class member, you will need to wrap it with std::bind or a lambda to bind the implicit 'this' argument, eg

class MyClass {
public:
MyClass()
: helper_(std::bind(MyClass::do_work, this, std::placeholders::_1))
{}
void do_work(std::atomic<bool>& running_flag) { ... }
WorkerThread helper_;
};

Definition at line 63 of file WorkerThread.hpp.

Constructor & Destructor Documentation

◆ WorkerThread() [1/3]

dunedaq::utilities::WorkerThread::WorkerThread ( std::function< void(std::atomic< bool > &)> do_work)
explicit

WorkerThread Constructor.

Parameters
do_workFunction to be executed in the thread

This constructor sets the defaults for the thread control variables

Definition at line 20 of file WorkerThread.cpp.

21 : m_thread_running(false)
22 , m_working_thread(nullptr)
23 , m_do_work(do_work)
24{
25}
std::unique_ptr< std::jthread > m_working_thread
std::atomic< bool > m_thread_running
std::function< void(std::atomic< bool > &)> m_do_work

◆ WorkerThread() [2/3]

dunedaq::utilities::WorkerThread::WorkerThread ( const WorkerThread & )
delete

WorkerThread is not copy-constructible.

◆ WorkerThread() [3/3]

dunedaq::utilities::WorkerThread::WorkerThread ( WorkerThread && )
delete

WorkerThread is not move-constructible.

Member Function Documentation

◆ operator=() [1/2]

WorkerThread & dunedaq::utilities::WorkerThread::operator= ( const WorkerThread & )
delete

WorkerThread is not copy-assginable.

◆ operator=() [2/2]

WorkerThread & dunedaq::utilities::WorkerThread::operator= ( WorkerThread && )
delete

WorkerThread is not move-assignable.

◆ start_working_thread()

void dunedaq::utilities::WorkerThread::start_working_thread ( const std::string & name = "noname")

Start the working thread (which executes the do_work() function)

Exceptions
ThreadingIssueif the thread is already running

Definition at line 28 of file WorkerThread.cpp.

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}
#define ERS_HERE
bool thread_running() const
Determine if the thread is currently running.
void warning(const Issue &issue)
Definition ers.hpp:115

◆ stop_working_thread()

void dunedaq::utilities::WorkerThread::stop_working_thread ( )

Stop the working thread.

Exceptions
ThreadingIssueIf the thread has not yet been started
ThreadingIssueIf the thread is not in the joinable state
ThreadingIssueIf an exception occurs during thread join

Definition at line 47 of file WorkerThread.cpp.

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}

◆ thread_running()

bool dunedaq::utilities::WorkerThread::thread_running ( ) const
inline

Determine if the thread is currently running.

Returns
Whether the thread is currently running

Definition at line 91 of file WorkerThread.hpp.

91{ return m_thread_running.load(); }

Member Data Documentation

◆ m_do_work

std::function<void(std::atomic<bool>&)> dunedaq::utilities::WorkerThread::m_do_work
private

Definition at line 101 of file WorkerThread.hpp.

◆ m_thread_running

std::atomic<bool> dunedaq::utilities::WorkerThread::m_thread_running
private

Definition at line 99 of file WorkerThread.hpp.

◆ m_working_thread

std::unique_ptr<std::jthread> dunedaq::utilities::WorkerThread::m_working_thread
private

Definition at line 100 of file WorkerThread.hpp.


The documentation for this class was generated from the following files: