RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
RST::Threads::ThreadPool Class Reference

Worker threads running tasks by priority, then in submission order. More...

#include <RST/threads/ThreadPool.hpp>

Public Types

enum class  Priority : uint8_t { Low = 0 , Normal = 1 , High = 2 }
 Higher priorities run first. More...
 

Public Member Functions

 ThreadPool (std::size_t numThreads=std::thread::hardware_concurrency(), std::string_view name="ThreadPool")
 Starts numThreads workers, named after name in debuggers on Linux and macOS.
 
 ~ThreadPool ()
 Runs the queued tasks, then joins the workers.
 
 ThreadPool (const ThreadPool &)=delete
 
ThreadPool & operator= (const ThreadPool &)=delete
 
 ThreadPool (ThreadPool &&)=delete
 
ThreadPool & operator= (ThreadPool &&)=delete
 
template<typename F , typename... Args>
auto submit (F &&fn, Priority priority=Priority::Normal, Args &&... args)
 Queues fn(args...) and returns a std::future of its result.
 
void pause () noexcept
 Stops starting new tasks; running tasks finish.
 
void resume () noexcept
 
bool isPaused () const noexcept
 
void waitForAll ()
 Blocks until every queued task has run; do not call it while paused.
 
std::size_t workerCount () const noexcept
 
std::string_view name () const noexcept
 

Detailed Description

Worker threads running tasks by priority, then in submission order.

ThreadPool pool(4);
std::future<int> sum = pool.submit([](int a, int b) { return a + b; }, ThreadPool::Priority::High, 2, 3);
Worker threads running tasks by priority, then in submission order.
Definition ThreadPool.hpp:24

Member Enumeration Documentation

◆ Priority

enum class RST::Threads::ThreadPool::Priority : uint8_t
strong

Higher priorities run first.

Enumerator
Low 
Normal 
High 

Constructor & Destructor Documentation

◆ ThreadPool() [1/3]

RST::Threads::ThreadPool::ThreadPool ( std::size_t  numThreads = std::thread::hardware_concurrency(),
std::string_view  name = "ThreadPool" 
)

Starts numThreads workers, named after name in debuggers on Linux and macOS.

Exceptions
std::invalid_argumentif numThreads is 0.

◆ ~ThreadPool()

RST::Threads::ThreadPool::~ThreadPool ( )

Runs the queued tasks, then joins the workers.

◆ ThreadPool() [2/3]

RST::Threads::ThreadPool::ThreadPool ( const ThreadPool &  )
delete

◆ ThreadPool() [3/3]

RST::Threads::ThreadPool::ThreadPool ( ThreadPool &&  )
delete

Member Function Documentation

◆ operator=() [1/2]

ThreadPool & RST::Threads::ThreadPool::operator= ( const ThreadPool &  )
delete

◆ operator=() [2/2]

ThreadPool & RST::Threads::ThreadPool::operator= ( ThreadPool &&  )
delete

◆ submit()

template<typename F , typename... Args>
auto RST::Threads::ThreadPool::submit ( F &&  fn,
Priority  priority = Priority::Normal,
Args &&...  args 
)
inline

Queues fn(args...) and returns a std::future of its result.

Exceptions
std::runtime_errorif the pool is being destroyed.

◆ pause()

void RST::Threads::ThreadPool::pause ( )
noexcept

Stops starting new tasks; running tasks finish.

◆ resume()

void RST::Threads::ThreadPool::resume ( )
noexcept

◆ isPaused()

bool RST::Threads::ThreadPool::isPaused ( ) const
noexcept

◆ waitForAll()

void RST::Threads::ThreadPool::waitForAll ( )

Blocks until every queued task has run; do not call it while paused.

◆ workerCount()

std::size_t RST::Threads::ThreadPool::workerCount ( ) const
inlinenoexcept

◆ name()

std::string_view RST::Threads::ThreadPool::name ( ) const
inlinenoexcept

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