ATLAS Offline Software
Public Member Functions | Private Member Functions | Private Attributes | List of all members
HLT::LoopThread Class Reference

Helper class to manage a long-running thread (duration of event loop) More...

#include <EventLoopUtils.h>

Collaboration diagram for HLT::LoopThread:

Public Member Functions

 LoopThread (std::function< void()> &&callback, int callbackInterval=-1)
 
 ~LoopThread ()
 
 LoopThread (const LoopThread &)=delete
 
 LoopThread (LoopThread &&)=delete
 
LoopThreadoperator= (const LoopThread &)=delete
 
LoopThreadoperator= (LoopThread &&)=delete
 
void start ()
 Keep notifying the thread until the callback is called for the first time (returns just before calling the callback) More...
 
void stop ()
 Flag the main loop to finish. More...
 
void wait ()
 Wait until main loop finishes. More...
 
std::condition_variable & cond ()
 
std::mutex & mutex ()
 

Private Member Functions

void waitForCond (std::unique_lock< std::mutex > &lock)
 Helper to wait for the condition. More...
 
void run ()
 Main function executed by the thread. More...
 

Private Attributes

std::condition_variable m_cond
 Condition for which the thread waits most of its lifetime. More...
 
std::mutex m_mutex
 Mutex used to notify the condition. More...
 
bool m_keepRunning {true}
 The thread's inner while-loop condition variable. More...
 
bool m_started {false}
 Flag whether the main loop of the thread has started and will listen to further notifications. More...
 
bool m_finished {false}
 Flag whether the main loop of the thread has finished. More...
 
std::function< void()> m_callback
 The callback executed in each step of the thread's inner while-loop. More...
 
int m_callbackIntervalMilliseconds {-1}
 If positive, call the callback periodically with this interval regardless of the m_cond. More...
 
std::unique_ptr< std::thread > m_thread
 The thread object. More...
 

Detailed Description

Helper class to manage a long-running thread (duration of event loop)

Definition at line 15 of file EventLoopUtils.h.

Constructor & Destructor Documentation

◆ LoopThread() [1/3]

HLT::LoopThread::LoopThread ( std::function< void()> &&  callback,
int  callbackInterval = -1 
)
inlineexplicit

Definition at line 62 of file EventLoopUtils.h.

63  : m_callback(std::move(callback)),
64  m_callbackIntervalMilliseconds(callbackInterval),
65  m_thread(std::make_unique<std::thread>([this]{run();})) {}

◆ ~LoopThread()

HLT::LoopThread::~LoopThread ( )
inline

Definition at line 67 of file EventLoopUtils.h.

67  {
68  wait();
69  }

◆ LoopThread() [2/3]

HLT::LoopThread::LoopThread ( const LoopThread )
delete

◆ LoopThread() [3/3]

HLT::LoopThread::LoopThread ( LoopThread &&  )
delete

Member Function Documentation

◆ cond()

std::condition_variable& HLT::LoopThread::cond ( )
inline

Definition at line 105 of file EventLoopUtils.h.

105 {return m_cond;}

◆ mutex()

std::mutex& HLT::LoopThread::mutex ( )
inline

Definition at line 106 of file EventLoopUtils.h.

106 {return m_mutex;}

◆ operator=() [1/2]

LoopThread& HLT::LoopThread::operator= ( const LoopThread )
delete

◆ operator=() [2/2]

LoopThread& HLT::LoopThread::operator= ( LoopThread &&  )
delete

◆ run()

void HLT::LoopThread::run ( )
inlineprivate

Main function executed by the thread.

Definition at line 44 of file EventLoopUtils.h.

44  {
45  if (!m_keepRunning) {return;}
46  std::unique_lock<std::mutex> lock{m_mutex};
47 
48  // first call outside the loop to set the m_started flag
49  waitForCond(lock);
50  m_started = true;
51  m_callback();
52 
53  // subsequent calls in a loop
54  while(m_keepRunning) {
55  waitForCond(lock);
56  m_callback();
57  }
58  m_finished = true;
59  }

◆ start()

void HLT::LoopThread::start ( )
inline

Keep notifying the thread until the callback is called for the first time (returns just before calling the callback)

Definition at line 79 of file EventLoopUtils.h.

79  {
80  while (!m_started) {
81  std::this_thread::sleep_for(std::chrono::milliseconds(3));
82  m_cond.notify_one();
83  }
84  }

◆ stop()

void HLT::LoopThread::stop ( )
inline

Flag the main loop to finish.

Definition at line 87 of file EventLoopUtils.h.

87  {
88  m_keepRunning=false;
89  m_cond.notify_all();
90  }

◆ wait()

void HLT::LoopThread::wait ( )
inline

Wait until main loop finishes.

Definition at line 93 of file EventLoopUtils.h.

93  {
94  // Nothing to do if thread already finished
95  if (m_thread==nullptr || !m_thread->joinable()) {return;}
96  // Keep notifying the condition until the loop finishes
97  while (!m_finished) {
98  std::this_thread::sleep_for(std::chrono::milliseconds(3));
99  m_cond.notify_all();
100  }
101  // Wait for the thread to return
102  m_thread->join();
103  }

◆ waitForCond()

void HLT::LoopThread::waitForCond ( std::unique_lock< std::mutex > &  lock)
inlineprivate

Helper to wait for the condition.

Definition at line 35 of file EventLoopUtils.h.

35  {
37  m_cond.wait(lock);
38  } else {
39  m_cond.wait_for(lock, std::chrono::milliseconds(m_callbackIntervalMilliseconds));
40  }
41  }

Member Data Documentation

◆ m_callback

std::function<void()> HLT::LoopThread::m_callback
private

The callback executed in each step of the thread's inner while-loop.

Definition at line 28 of file EventLoopUtils.h.

◆ m_callbackIntervalMilliseconds

int HLT::LoopThread::m_callbackIntervalMilliseconds {-1}
private

If positive, call the callback periodically with this interval regardless of the m_cond.

Definition at line 30 of file EventLoopUtils.h.

◆ m_cond

std::condition_variable HLT::LoopThread::m_cond
private

Condition for which the thread waits most of its lifetime.

Definition at line 18 of file EventLoopUtils.h.

◆ m_finished

bool HLT::LoopThread::m_finished {false}
private

Flag whether the main loop of the thread has finished.

Definition at line 26 of file EventLoopUtils.h.

◆ m_keepRunning

bool HLT::LoopThread::m_keepRunning {true}
private

The thread's inner while-loop condition variable.

Definition at line 22 of file EventLoopUtils.h.

◆ m_mutex

std::mutex HLT::LoopThread::m_mutex
private

Mutex used to notify the condition.

Definition at line 20 of file EventLoopUtils.h.

◆ m_started

bool HLT::LoopThread::m_started {false}
private

Flag whether the main loop of the thread has started and will listen to further notifications.

Definition at line 24 of file EventLoopUtils.h.

◆ m_thread

std::unique_ptr<std::thread> HLT::LoopThread::m_thread
private

The thread object.

Definition at line 32 of file EventLoopUtils.h.


The documentation for this class was generated from the following file:
HLT::LoopThread::m_callback
std::function< void()> m_callback
The callback executed in each step of the thread's inner while-loop.
Definition: EventLoopUtils.h:28
HLT::LoopThread::waitForCond
void waitForCond(std::unique_lock< std::mutex > &lock)
Helper to wait for the condition.
Definition: EventLoopUtils.h:35
HLT::LoopThread::wait
void wait()
Wait until main loop finishes.
Definition: EventLoopUtils.h:93
runLayerRecalibration.callback
callback
Definition: runLayerRecalibration.py:64
HLT::LoopThread::m_keepRunning
bool m_keepRunning
The thread's inner while-loop condition variable.
Definition: EventLoopUtils.h:22
HLT::LoopThread::m_finished
bool m_finished
Flag whether the main loop of the thread has finished.
Definition: EventLoopUtils.h:26
HLT::LoopThread::m_mutex
std::mutex m_mutex
Mutex used to notify the condition.
Definition: EventLoopUtils.h:20
HLT::LoopThread::run
void run()
Main function executed by the thread.
Definition: EventLoopUtils.h:44
HLT::LoopThread::m_started
bool m_started
Flag whether the main loop of the thread has started and will listen to further notifications.
Definition: EventLoopUtils.h:24
HLT::LoopThread::m_thread
std::unique_ptr< std::thread > m_thread
The thread object.
Definition: EventLoopUtils.h:32
HLT::LoopThread::m_callbackIntervalMilliseconds
int m_callbackIntervalMilliseconds
If positive, call the callback periodically with this interval regardless of the m_cond.
Definition: EventLoopUtils.h:30
HLT::LoopThread::m_cond
std::condition_variable m_cond
Condition for which the thread waits most of its lifetime.
Definition: EventLoopUtils.h:18