0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
clanguml::util::thread_pool_executor Class Reference

Simple thread pool executor for parallelizing diagram generation. More...

Detailed Description

Simple thread pool executor for parallelizing diagram generation.

Definition at line 30 of file thread_pool_executor.h.

#include <thread_pool_executor.h>

Public Member Functions

 thread_pool_executor (unsigned int pool_size)
 Constructor.
 
 thread_pool_executor (const thread_pool_executor &)=delete
 
 thread_pool_executor (thread_pool_executor &&)=delete
 
thread_pool_executoroperator= (const thread_pool_executor &)=delete
 
thread_pool_executoroperator= (thread_pool_executor &&)=delete
 
 ~thread_pool_executor ()
 
std::future< void > add (std::function< void()> &&task)
 Add a task to run on the pool.
 
void stop ()
 Join all active threads in the pool.
 

Private Member Functions

void worker ()
 Main worker pool thread method - take task from queue and execute.
 
std::packaged_task< void()> get ()
 

Private Attributes

std::atomic_bool done_
 
std::deque< std::packaged_task< void()> > tasks_
 
std::mutex tasks_mutex_
 
std::condition_variable tasks_cond_
 
std::vector< std::thread > threads_
 

Constructor & Destructor Documentation

◆ thread_pool_executor() [1/3]

clanguml::util::thread_pool_executor::thread_pool_executor ( unsigned int  pool_size)
explicit

Constructor.

Parameters
pool_sizeNumber of threads in the pool

Definition at line 23 of file thread_pool_executor.cc.

24 : done_{false}
25{
26 if (pool_size == 0U)
27 pool_size = std::thread::hardware_concurrency();
28
29 for (auto i = 0U; i < pool_size; i++) {
30 threads_.emplace_back(&thread_pool_executor::worker, this);
31 }
32}

◆ thread_pool_executor() [2/3]

clanguml::util::thread_pool_executor::thread_pool_executor ( const thread_pool_executor )
delete

◆ thread_pool_executor() [3/3]

clanguml::util::thread_pool_executor::thread_pool_executor ( thread_pool_executor &&  )
delete

◆ ~thread_pool_executor()

clanguml::util::thread_pool_executor::~thread_pool_executor ( )

Definition at line 34 of file thread_pool_executor.cc.

34{ stop(); }

Member Function Documentation

◆ add()

std::future< void > clanguml::util::thread_pool_executor::add ( std::function< void()> &&  task)

Add a task to run on the pool.

Parameters
taskFunction to execute
Returns
Future, allowing awaiting the result

Definition at line 36 of file thread_pool_executor.cc.

37{
38 std::unique_lock<std::mutex> l(tasks_mutex_);
39
40 std::packaged_task<void()> ptask{std::move(task)};
41 auto res = ptask.get_future();
42
43 tasks_.emplace_back(std::move(ptask));
44
45 l.unlock();
46 tasks_cond_.notify_one();
47
48 return res;
49}

◆ get()

std::packaged_task< void()> clanguml::util::thread_pool_executor::get ( )
private

Definition at line 73 of file thread_pool_executor.cc.

74{
75 std::unique_lock<std::mutex> l(tasks_mutex_);
76
77 while (tasks_.empty()) {
78 if (done_)
79 throw std::runtime_error("Thread pool closing...");
80
81 tasks_cond_.wait_for(l, std::chrono::seconds(1));
82 }
83
84 auto res = std::move(tasks_.front());
85 tasks_.pop_front();
86 return res;
87}

◆ operator=() [1/2]

thread_pool_executor & clanguml::util::thread_pool_executor::operator= ( const thread_pool_executor )
delete

◆ operator=() [2/2]

thread_pool_executor & clanguml::util::thread_pool_executor::operator= ( thread_pool_executor &&  )
delete

◆ stop()

void clanguml::util::thread_pool_executor::stop ( )

Join all active threads in the pool.

Definition at line 51 of file thread_pool_executor.cc.

52{
53 done_ = true;
54 for (auto &thread : threads_) {
55 if (thread.joinable())
56 thread.join();
57 }
58}

◆ worker()

void clanguml::util::thread_pool_executor::worker ( )
private

Main worker pool thread method - take task from queue and execute.

Definition at line 60 of file thread_pool_executor.cc.

61{
62 try {
63 while (!done_) {
64 auto task = get();
65
66 task();
67 }
68 }
69 catch (std::runtime_error &e) {
70 }
71}

Member Data Documentation

◆ done_

std::atomic_bool clanguml::util::thread_pool_executor::done_
private

Definition at line 67 of file thread_pool_executor.h.

◆ tasks_

std::deque<std::packaged_task<void()> > clanguml::util::thread_pool_executor::tasks_
private

Definition at line 68 of file thread_pool_executor.h.

◆ tasks_cond_

std::condition_variable clanguml::util::thread_pool_executor::tasks_cond_
private

Definition at line 70 of file thread_pool_executor.h.

◆ tasks_mutex_

std::mutex clanguml::util::thread_pool_executor::tasks_mutex_
private

Definition at line 69 of file thread_pool_executor.h.

◆ threads_

std::vector<std::thread> clanguml::util::thread_pool_executor::threads_
private

Definition at line 71 of file thread_pool_executor.h.


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