Doxygen
Loading...
Searching...
No Matches
ThreadPool Class Reference

Class managing a pool of worker threads. More...

#include <src/threadpool.h>

Public Member Functions

 ThreadPool (std::size_t N=1)
 start N threads in the thread pool.
 
 ~ThreadPool ()
 deletes the thread pool by finishing all threads
 
 ThreadPool (const ThreadPool &)=delete
 
ThreadPooloperator= (const ThreadPool &)=delete
 
 ThreadPool (ThreadPool &&)=delete
 
ThreadPooloperator= (ThreadPool &&)=delete
 
template<class F, typename ... Args>
auto queue (F &&f, Args &&... args) -> std::future< decltype(f(args...))>
 Queue the callable function f for the threads to execute.
 
void finish ()
 finish enques a "stop the thread" message for every thread, then waits for them to finish
 

Private Member Functions

void threadTask ()
 

Private Attributes

std::mutex m_mutex
 
std::condition_variable m_cond
 
std::deque< std::function< void()> > m_work
 
std::vector< std::future< void > > m_finished
 

Detailed Description

Class managing a pool of worker threads.

Work can be queued by passing a function to queue(). A future will be returned that can be used to obtain the result of the function after execution.

Usage example:

ThreadPool pool(10);
std::vector< std::future< int > > results;
for (int i=0;i<10;i++)
{
auto run = [](int i) { return i*i; };
results.emplace_back(pool.queue(std::bind(run,i)));
}
for (auto &f : results)
{
printf("Result %d:\n", f.get());
}
ThreadPool(std::size_t N=1)
start N threads in the thread pool.
Definition threadpool.h:51

Definition at line 47 of file threadpool.h.

Constructor & Destructor Documentation

◆ ThreadPool() [1/3]

ThreadPool::ThreadPool ( std::size_t N = 1)
inline

start N threads in the thread pool.

Definition at line 51 of file threadpool.h.

52 {
53 for (std::size_t i = 0; i < N; ++i)
54 {
55 // each thread is a std::async running thread_task():
56 m_finished.push_back(
57 std::async(
58 std::launch::async,
59 [this]{ threadTask(); }
60 )
61 );
62 }
63 }
void threadTask()
Definition threadpool.h:116
std::vector< std::future< void > > m_finished
Definition threadpool.h:148

References m_finished, and threadTask().

Referenced by operator=(), operator=(), ThreadPool(), and ThreadPool().

◆ ~ThreadPool()

ThreadPool::~ThreadPool ( )
inline

deletes the thread pool by finishing all threads

Definition at line 65 of file threadpool.h.

66 {
67 finish();
68 }
void finish()
finish enques a "stop the thread" message for every thread, then waits for them to finish
Definition threadpool.h:100

References finish().

◆ ThreadPool() [2/3]

ThreadPool::ThreadPool ( const ThreadPool & )
delete

References ThreadPool().

◆ ThreadPool() [3/3]

ThreadPool::ThreadPool ( ThreadPool && )
delete

References ThreadPool().

Member Function Documentation

◆ finish()

void ThreadPool::finish ( )
inline

finish enques a "stop the thread" message for every thread, then waits for them to finish

Definition at line 100 of file threadpool.h.

101 {
102 {
103 std::unique_lock<std::mutex> l(m_mutex);
104 for(auto&& u : m_finished)
105 {
106 (void)u; //unused_variable, to silence the compiler warning about unused variables
107 m_work.emplace_back(); // insert empty function object to signal abort
108 }
109 }
110 m_cond.notify_all();
111 m_finished.clear();
112 }
std::deque< std::function< void()> > m_work
Definition threadpool.h:145
std::mutex m_mutex
Definition threadpool.h:141
std::condition_variable m_cond
Definition threadpool.h:142

References m_cond, m_finished, m_mutex, and m_work.

Referenced by ~ThreadPool().

◆ operator=() [1/2]

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

References ThreadPool().

◆ operator=() [2/2]

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

References ThreadPool().

◆ queue()

template<class F, typename ... Args>
auto ThreadPool::queue ( F && f,
Args &&... args ) -> std::future<decltype(f(args...))>
inline

Queue the callable function f for the threads to execute.

A future of the return type of the function is returned to capture the result.

Definition at line 77 of file threadpool.h.

78 {
79 // We wrap the function object into a packaged task, splitting
80 // execution from the return value.
81 // Since the packaged_task object is not copyable, we create it on the heap
82 // and capture it via a shared pointer in a lambda and then assign that lambda
83 // to a std::function.
84 using RetType = decltype(f(args...));
85 auto ptr = std::make_shared< std::packaged_task<RetType()> >(std::forward<F>(f), std::forward<Args>(args)...);
86 auto taskFunc = [ptr]() { if (ptr->valid()) (*ptr)(); };
87
88 auto r=ptr->get_future(); // get the return value before we hand off the task
89 {
90 std::unique_lock<std::mutex> l(m_mutex);
91 m_work.emplace_back(taskFunc);
92 m_cond.notify_one(); // wake a thread to work on the task
93 }
94
95 return r; // return the future result of the task
96 }

References m_cond, m_mutex, and m_work.

Referenced by computeTooltipTexts(), FormulaManager::createFormulasTexFile(), generateDocsForClassList(), generateFileDocs(), generateFileSources(), generateJSTreeFiles(), generateNamespaceClassDocs(), parseFilesMultiThreading(), and writeJavaScriptSearchIndex().

◆ threadTask()

void ThreadPool::threadTask ( )
inlineprivate

Definition at line 116 of file threadpool.h.

117 {
118 while(true)
119 {
120 // pop a task off the queue:
121 std::function<void()> f;
122 {
123 // usual thread-safe queue code:
124 std::unique_lock<std::mutex> l(m_mutex);
125 if (m_work.empty())
126 {
127 m_cond.wait(l,[&]{return !m_work.empty();});
128 }
129 f = std::move(m_work.front());
130 m_work.pop_front();
131 }
132 // if the function is empty, it means we are asked to abort
133 if (!f) return;
134 // run the task
135 f();
136 }
137 }

References m_cond, m_mutex, and m_work.

Referenced by ThreadPool().

Member Data Documentation

◆ m_cond

std::condition_variable ThreadPool::m_cond
private

Definition at line 142 of file threadpool.h.

Referenced by finish(), queue(), and threadTask().

◆ m_finished

std::vector< std::future<void> > ThreadPool::m_finished
private

Definition at line 148 of file threadpool.h.

Referenced by finish(), and ThreadPool().

◆ m_mutex

std::mutex ThreadPool::m_mutex
private

Definition at line 141 of file threadpool.h.

Referenced by finish(), queue(), and threadTask().

◆ m_work

std::deque< std::function<void()> > ThreadPool::m_work
private

Definition at line 145 of file threadpool.h.

Referenced by finish(), queue(), and threadTask().


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