Public Member Functions | List of all members
rapidsmpf::detail::PausableThreadLoop Class Reference

A thread loop that can be paused, resumed, and stopped. More...

#include <pausable_thread_loop.hpp>

Public Member Functions

 PausableThreadLoop (std::function< void()> func, Duration sleep=std::chrono::seconds{0})
 Constructs a thread to run the specified function in a loop. More...
 
bool is_running () const noexcept
 Checks if the thread is currently running (not paused or stopped). More...
 
void pause_nb () noexcept
 Pauses the execution of the thread. More...
 
void pause () noexcept
 Pauses the execution of the thread. More...
 
bool resume () noexcept
 Resumes execution of the thread after being paused. More...
 
bool stop () noexcept
 Stops the execution of the thread and joins it. More...
 

Detailed Description

A thread loop that can be paused, resumed, and stopped.

This class runs a provided function repeatedly in a separate thread.

Definition at line 21 of file pausable_thread_loop.hpp.

Constructor & Destructor Documentation

◆ PausableThreadLoop()

rapidsmpf::detail::PausableThreadLoop::PausableThreadLoop ( std::function< void()>  func,
Duration  sleep = std::chrono::seconds{0} 
)

Constructs a thread to run the specified function in a loop.

The loop will execute the given function repeatedly in a separate thread. The thread starts in a paused state and will remain paused until the resume() method is called.

If a sleep duration is provided, the thread will sleep for the specified duration between executions of the task.

A short sleep is introduced when running under Valgrind to avoid potential issues with thread starvation.

Parameters
funcThe function to execute repeatedly in the thread.
sleepThe duration to sleep between executions of the task. By default, the thread yields execution instead of sleeping.

Member Function Documentation

◆ is_running()

bool rapidsmpf::detail::PausableThreadLoop::is_running ( ) const
noexcept

Checks if the thread is currently running (not paused or stopped).

Note
If false, the loop function might still be in the middle of running its last iteration before being paused or stopped.
Returns
True if the thread is running, false if paused or stopped.

◆ pause()

void rapidsmpf::detail::PausableThreadLoop::pause ( )
noexcept

Pauses the execution of the thread.

The thread will stop executing the loop function until resume() is called.

Note
Pausing the thread does not interrupt the current iteration.

◆ pause_nb()

void rapidsmpf::detail::PausableThreadLoop::pause_nb ( )
noexcept

Pauses the execution of the thread.

The thread will stop executing the loop function until resume() is called.

Note
This function is non-blocking and will let the loop function finish its current execution asynchronously.
Warning
After calling pause_nb the thread is not paused until the state has changed to State::PAUSED, so immediately notifying a thread that is waiting on is_running will not necessarily wake it.

◆ resume()

bool rapidsmpf::detail::PausableThreadLoop::resume ( )
noexcept

Resumes execution of the thread after being paused.

Calling resume on an already running loop is a no-op and is allowed.

Returns
True if the state is changed to Running, false otherwise.

◆ stop()

bool rapidsmpf::detail::PausableThreadLoop::stop ( )
noexcept

Stops the execution of the thread and joins it.

Once stopped, the thread cannot be resumed.

Note
This function is blocking and will wait on the loop function to finish its current execution.
Returns
True if the thread is stopped by this call, false otherwise (if it was already stopping/stopped).

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