LMMS
Loading...
Searching...
No Matches
lmms::ThreadPool Class Reference

A thread pool that can be used for asynchronous processing. More...

#include <ThreadPool.h>

Public Member Functions

 ~ThreadPool ()
template<typename Fn, typename... Args>
auto enqueue (Fn &&fn, Args &&... args) -> std::future< std::invoke_result_t< Fn, Args... > >
 Enqueue function fn with arguments args to be ran asynchronously.
auto numWorkers () const -> size_t
 Return the number of worker threads used.

Static Public Member Functions

static auto instance () -> ThreadPool &
 Return the global ThreadPool instance.

Private Member Functions

 ThreadPool (size_t numWorkers)
void run ()

Private Attributes

std::vector< std::thread > m_workers
std::queue< std::function< void()> > m_queue
std::atomic< bool > m_done = false
std::condition_variable m_runCond
std::mutex m_runMutex

Static Private Attributes

static size_t s_numWorkers = std::thread::hardware_concurrency()

Detailed Description

A thread pool that can be used for asynchronous processing.

Constructor & Destructor Documentation

◆ ~ThreadPool()

lmms::ThreadPool::~ThreadPool ( )

Destroys the ThreadPool object. This blocks until all workers have finished executing.

◆ ThreadPool()

lmms::ThreadPool::ThreadPool ( size_t numWorkers)
private

Member Function Documentation

◆ enqueue()

template<typename Fn, typename... Args>
auto lmms::ThreadPool::enqueue ( Fn && fn,
Args &&... args ) -> std::future< std::invoke_result_t< Fn, Args... > >
inline

Enqueue function fn with arguments args to be ran asynchronously.

◆ instance()

auto lmms::ThreadPool::instance ( ) -> ThreadPool &
static

Return the global ThreadPool instance.

◆ numWorkers()

auto lmms::ThreadPool::numWorkers ( ) const -> size_t

Return the number of worker threads used.

◆ run()

void lmms::ThreadPool::run ( )
private

Member Data Documentation

◆ m_done

std::atomic<bool> lmms::ThreadPool::m_done = false
private

◆ m_queue

std::queue<std::function<void()> > lmms::ThreadPool::m_queue
private

◆ m_runCond

std::condition_variable lmms::ThreadPool::m_runCond
private

◆ m_runMutex

std::mutex lmms::ThreadPool::m_runMutex
private

◆ m_workers

std::vector<std::thread> lmms::ThreadPool::m_workers
private

◆ s_numWorkers

size_t lmms::ThreadPool::s_numWorkers = std::thread::hardware_concurrency()
inlinestaticprivate

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