Skip to main content

The ThreadPool Class Reference

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

Declaration

class ThreadPool { ... }

Included Headers

#include <src/threadpool.h>

Public Constructors Index

ThreadPool (std::size_t N=1)

start N threads in the thread pool. More...

ThreadPool (const ThreadPool &)=delete
ThreadPool (ThreadPool &&)=delete

Public Destructor Index

~ThreadPool ()

deletes the thread pool by finishing all threads More...

Public Operators Index

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

Public Member Functions Index

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. More...

voidfinish ()

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

Private Member Functions Index

voidthreadTask ()

Private Member Attributes Index

std::mutexm_mutex
std::condition_variablem_cond
std::deque< std::function< void()> >m_work
std::vector< std::future< void > >m_finished

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());
 }

Definition at line 47 of file threadpool.h.

Public Constructors

ThreadPool()

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

start N threads in the thread pool.

Definition at line 51 of file threadpool.h.

51 ThreadPool(std::size_t N=1)
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 }

References m_finished and threadTask.

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

ThreadPool()

ThreadPool::ThreadPool (const ThreadPool &)
delete

Definition at line 69 of file threadpool.h.

Reference ThreadPool.

ThreadPool()

ThreadPool::ThreadPool (ThreadPool &&)
delete

Definition at line 71 of file threadpool.h.

Reference ThreadPool.

Public Destructor

~ThreadPool()

ThreadPool::~ThreadPool ()
inline

deletes the thread pool by finishing all threads

Definition at line 65 of file threadpool.h.

66 {
67 finish();
68 }

Reference finish.

Public Operators

operator=()

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

Definition at line 70 of file threadpool.h.

Reference ThreadPool.

operator=()

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

Definition at line 72 of file threadpool.h.

Reference ThreadPool.

Public Member Functions

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.

100 void finish()
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 }

References m_cond, m_finished, m_mutex and m_work.

Referenced by ~ThreadPool.

queue()

template <class F, typename ... Args>
std::future< decltype(f(args...))> ThreadPool::queue (F && f, Args &&... 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.

77 auto queue(F&& f, Args&&... args) -> std::future<decltype(f(args...))>
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.

Private Member Functions

threadTask()

void ThreadPool::threadTask ()
inline

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.

Private Member Attributes

m_cond

std::condition_variable ThreadPool::m_cond

Definition at line 142 of file threadpool.h.

142 std::condition_variable m_cond;

Referenced by finish, queue and threadTask.

m_finished

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

Definition at line 148 of file threadpool.h.

148 std::vector< std::future<void> > m_finished;

Referenced by finish and ThreadPool.

m_mutex

std::mutex ThreadPool::m_mutex

Definition at line 141 of file threadpool.h.

141 std::mutex m_mutex;

Referenced by finish, queue and threadTask.

m_work

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

Definition at line 145 of file threadpool.h.

145 std::deque< std::function<void()> > m_work;

Referenced by finish, queue and threadTask.


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


Generated via doxygen2docusaurus by Doxygen 1.14.0.