Flow
Documentation for the Flow C++ Library
Loading...
Searching...
No Matches
flow::SimpleThreadPool< ThreadSafeQueue > Class Template Reference

A simple thread pool with fixed number of worker threads. All the threads poll tasks from a single queue. If all the threads are waiting, then the user must manually call runPendingTask() to resolve deadlock. More...

#include <flow_simple_thread_pool.h>

Public Member Functions

 SimpleThreadPool (std::size_t threadCount=std::thread::hardware_concurrency() - 1)
std::size_t poolSize () const
template<typename Callable, typename ... Args>
std::future< std::invoke_result_t< Callable, Args &&... > > submit (Callable callable, Args &&... args)
 Submit a task to the thread pool queue.
void runPendingTask ()
 Try run a task from the pool queue. If the queue is empty, then it yields the thread. This helps resolves thread deadlock due to dependency waiting.

Private Member Functions

void runTasks (std::stop_token token)

Private Attributes

flow::Vector< std::jthread > threads_
ThreadSafeQueue queue_

Detailed Description

template<typename ThreadSafeQueue = ConcurrentQueue<ThreadTask>>
class flow::SimpleThreadPool< ThreadSafeQueue >

A simple thread pool with fixed number of worker threads. All the threads poll tasks from a single queue. If all the threads are waiting, then the user must manually call runPendingTask() to resolve deadlock.

Definition at line 15 of file flow_simple_thread_pool.h.

Constructor & Destructor Documentation

◆ SimpleThreadPool()

template<typename ThreadSafeQueue = ConcurrentQueue<ThreadTask>>
flow::SimpleThreadPool< ThreadSafeQueue >::SimpleThreadPool ( std::size_t threadCount = std::thread::hardware_concurrency() - 1)
inlineexplicit

Definition at line 20 of file flow_simple_thread_pool.h.

20 {
21 threads_.reserve(threadCount);
22 for (std::size_t i = 0; i < threadCount; ++i) {
24 }
25 }
A simple thread pool with fixed number of worker threads. All the threads poll tasks from a single qu...
flow::Vector< std::jthread > threads_

References runTasks(), and threads_.

Member Function Documentation

◆ poolSize()

template<typename ThreadSafeQueue = ConcurrentQueue<ThreadTask>>
std::size_t flow::SimpleThreadPool< ThreadSafeQueue >::poolSize ( ) const
inline
Returns
The number of worker threads.

Definition at line 28 of file flow_simple_thread_pool.h.

28 {
29 return threads_.size();
30 }

References threads_.

◆ runPendingTask()

template<typename ThreadSafeQueue = ConcurrentQueue<ThreadTask>>
void flow::SimpleThreadPool< ThreadSafeQueue >::runPendingTask ( )
inline

Try run a task from the pool queue. If the queue is empty, then it yields the thread. This helps resolves thread deadlock due to dependency waiting.

Definition at line 47 of file flow_simple_thread_pool.h.

47 {
48 if (auto task = queue_.tryPop(); task) {
49 task->execute();
50 } else {
52 }
53 }

References queue_.

Referenced by runTasks().

◆ runTasks()

template<typename ThreadSafeQueue = ConcurrentQueue<ThreadTask>>
void flow::SimpleThreadPool< ThreadSafeQueue >::runTasks ( std::stop_token token)
inlineprivate

Definition at line 56 of file flow_simple_thread_pool.h.

56 {
57 while (!token.stop_requested()) {
59 }
60 }
void runPendingTask()
Try run a task from the pool queue. If the queue is empty, then it yields the thread....

References runPendingTask().

Referenced by SimpleThreadPool().

◆ submit()

template<typename ThreadSafeQueue = ConcurrentQueue<ThreadTask>>
template<typename Callable, typename ... Args>
std::future< std::invoke_result_t< Callable, Args &&... > > flow::SimpleThreadPool< ThreadSafeQueue >::submit ( Callable callable,
Args &&... args )
inline

Submit a task to the thread pool queue.

Parameters
callableThe callable.
...argsThe callable arguments.

Definition at line 36 of file flow_simple_thread_pool.h.

References queue_.

Member Data Documentation

◆ queue_

template<typename ThreadSafeQueue = ConcurrentQueue<ThreadTask>>
ThreadSafeQueue flow::SimpleThreadPool< ThreadSafeQueue >::queue_
private

Definition at line 17 of file flow_simple_thread_pool.h.

Referenced by runPendingTask(), and submit().

◆ threads_

template<typename ThreadSafeQueue = ConcurrentQueue<ThreadTask>>
flow::Vector<std::jthread> flow::SimpleThreadPool< ThreadSafeQueue >::threads_
private

Definition at line 16 of file flow_simple_thread_pool.h.

Referenced by poolSize(), and SimpleThreadPool().


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