Doxygen
ThreadPool类 参考

Class managing a pool of worker threads. 更多...

#include <threadpool.h>

Public 成员函数

 ThreadPool (std::size_t N=1)
 start N threads in the thread pool. 更多...
 
 ~ThreadPool ()
 deletes the thread pool by finishing all threads 更多...
 
template<class F , class R = std::result_of_t<F&()>>
std::future< R > queue (F &&f)
 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 成员函数

template<typename ... Args>
void unused_variable (Args &&...args)
 
void threadTask ()
 

Private 属性

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

详细描述

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.h47 行定义.

构造及析构函数说明

◆ ThreadPool()

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

start N threads in the thread pool.

在文件 threadpool.h64 行定义.

66  {
67  finish();
68  }
69 
70  /// Queue the callable function \a f for the threads to execute.
71  /// A future of the return type of the function is returned to capture the result.
72  template<class F, class R=std::result_of_t<F&()> >
73  std::future<R> queue(F&& f)
74  {
75  // We wrap the function object into a packaged task, splitting
76  // execution from the return value.

引用了 finish().

◆ ~ThreadPool()

ThreadPool::~ThreadPool ( )
inline

deletes the thread pool by finishing all threads

在文件 threadpool.h78 行定义.

81  { if (ptr->valid()) (*ptr)(); };

成员函数说明

◆ finish()

void ThreadPool::finish ( )
inline

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

在文件 threadpool.h108 行定义.

108  :
109 
110  // helper to silence the compiler warning about unused variables
111  template <typename ...Args>
112  void unused_variable(Args&& ...args) { (void)(sizeof...(args)); }
113 
114  // the work that a worker thread does:
115  void threadTask()
116  {
117  while(true)
118  {
119  // pop a task off the queue:
120  std::function<void()> f;

被这些函数引用 ThreadPool().

◆ queue()

template<class F , class R = std::result_of_t<F&()>>
std::future<R> ThreadPool::queue ( F &&  f)
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.

在文件 threadpool.h86 行定义.

96  {
97  {
98  std::unique_lock<std::mutex> l(m_mutex);
99  for(auto&& u : m_finished)
100  {
101  unused_variable(u);
102  m_work.push_back({}); // insert empty function object to signal abort
103  }
104  }

被这些函数引用 generateClassList(), generateFileDocs(), generateFileSources(), generateNamespaceClassDocs() , 以及 parseFilesMultiThreading().

◆ threadTask()

void ThreadPool::threadTask ( )
inlineprivate

在文件 threadpool.h128 行定义.

◆ unused_variable()

template<typename ... Args>
void ThreadPool::unused_variable ( Args &&...  args)
inlineprivate

在文件 threadpool.h125 行定义.

125 {

引用了 m_cond , 以及 m_work.

类成员变量说明

◆ m_cond

std::condition_variable ThreadPool::m_cond
private

在文件 threadpool.h154 行定义.

被这些函数引用 unused_variable().

◆ m_finished

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

在文件 threadpool.h160 行定义.

◆ m_mutex

std::mutex ThreadPool::m_mutex
private

在文件 threadpool.h153 行定义.

◆ m_work

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

在文件 threadpool.h157 行定义.

被这些函数引用 unused_variable().


该类的文档由以下文件生成:
ThreadPool::queue
std::future< R > queue(F &&f)
Queue the callable function f for the threads to execute.
Definition: threadpool.h:86
ThreadPool
Class managing a pool of worker threads.
Definition: threadpool.h:47
ThreadPool::m_work
std::deque< std::function< void()> > m_work
Definition: threadpool.h:157
ThreadPool::m_finished
std::vector< std::future< void > > m_finished
Definition: threadpool.h:160
ThreadPool::threadTask
void threadTask()
Definition: threadpool.h:128
ThreadPool::m_mutex
std::mutex m_mutex
Definition: threadpool.h:153
ThreadPool::finish
void finish()
finish enques a "stop the thread" message for every thread, then waits for them to finish
Definition: threadpool.h:108
ThreadPool::unused_variable
void unused_variable(Args &&...args)
Definition: threadpool.h:125