DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
ReusableThread.hpp
Go to the documentation of this file.
1
11#ifndef UTILITIES_INCLUDE_UTILITIES_REUSABLETHREAD_HPP_
12#define UTILITIES_INCLUDE_UTILITIES_REUSABLETHREAD_HPP_
13
14#include <atomic>
15#include <chrono>
16#include <condition_variable>
17#include <functional>
18#include <mutex>
19#include <string>
20#include <thread>
21
22namespace dunedaq {
23namespace utilities {
24
26{
27public:
28 explicit ReusableThread(int threadid = 0);
29
31
32 ReusableThread(const ReusableThread&) = delete;
36
37 // Set thread ID
38 void set_thread_id(int tid) { m_thread_id = tid; }
39
40 // Get thread ID
41 int get_thread_id() const { return m_thread_id; }
42
43 // Set name for pthread handle
44 void set_name(const std::string& name, int tid);
45
46 // Pin thread to CPU
47 void set_pin(int cpuid);
48
49 // Check for completed task execution
50 bool get_readiness() const { return m_task_executed; }
51
52 // Set task to be executed
53 template<typename Function, typename... Args>
54 bool set_work(Function&& f, Args&&... args)
55 {
56 if (!m_task_assigned && m_task_executed.exchange(false)) {
57 m_task = std::bind(f, args...);
58 m_task_assigned = true;
59 m_cv.notify_all();
60 return true;
61 }
62 return false;
63 }
64
65private:
66 // Internals
68 std::atomic<bool> m_task_executed;
69 std::atomic<bool> m_task_assigned;
70 std::atomic<bool> m_thread_quit;
71 std::atomic<bool> m_worker_done;
72 std::atomic<bool> m_named;
73 std::function<void()> m_task;
74
75 // Locks
76 std::mutex m_mtx;
77 std::condition_variable m_cv;
78 std::thread m_thread;
79
80 // Actual worker thread
81 void thread_worker();
82};
83
84} // namespace utilities
85} // namespace dunedaq
86
87#endif // UTILITIES_INCLUDE_UTILITIES_REUSABLETHREAD_HPP_
bool set_work(Function &&f, Args &&... args)
ReusableThread & operator=(const ReusableThread &)=delete
ReusableThread is not copy-assginable.
ReusableThread(const ReusableThread &)=delete
ReusableThread is not copy-constructible.
void set_name(const std::string &name, int tid)
ReusableThread & operator=(ReusableThread &&)=delete
ReusableThread is not move-assignable.
ReusableThread(ReusableThread &&)=delete
ReusableThread is not move-constructible.
Including Qt Headers.