#include <ctpl.h>
Definition at line 48 of file ctpl.h.
◆ thread_pool() [1/4]
ctpl::thread_pool::thread_pool |
( |
| ) |
|
|
inline |
Definition at line 52 of file ctpl.h.
#define _ctplThreadPoolLength_
◆ thread_pool() [2/4]
Definition at line 53 of file ctpl.h.
53 : q(queueSize) { this->init(); this->
resize(nThreads); }
void resize(int nThreads)
◆ ~thread_pool() [1/2]
ctpl::thread_pool::~thread_pool |
( |
| ) |
|
|
inline |
Definition at line 56 of file ctpl.h.
void stop(bool isWait=false)
◆ thread_pool() [3/4]
ctpl::thread_pool::thread_pool |
( |
| ) |
|
|
inline |
◆ thread_pool() [4/4]
ctpl::thread_pool::thread_pool |
( |
int |
nThreads | ) |
|
|
inline |
Definition at line 76 of file ctpl_stl.h.
76 { this->init(); this->
resize(nThreads); }
void resize(int nThreads)
◆ ~thread_pool() [2/2]
ctpl::thread_pool::~thread_pool |
( |
| ) |
|
|
inline |
Definition at line 79 of file ctpl_stl.h.
void stop(bool isWait=false)
◆ clear_queue() [1/2]
void ctpl::thread_pool::clear_queue |
( |
| ) |
|
|
inline |
Definition at line 99 of file ctpl.h.
100 std::function<void(int id)> * _f;
101 while (this->q.pop(_f))
◆ clear_queue() [2/2]
void ctpl::thread_pool::clear_queue |
( |
| ) |
|
|
inline |
Definition at line 122 of file ctpl_stl.h.
123 std::function<void(int id)> * _f;
124 while (this->q.pop(_f))
◆ get_thread() [1/2]
std::thread& ctpl::thread_pool::get_thread |
( |
int |
i | ) |
|
|
inline |
Definition at line 65 of file ctpl.h.
65 {
return *this->threads[
i]; }
◆ get_thread() [2/2]
std::thread& ctpl::thread_pool::get_thread |
( |
int |
i | ) |
|
|
inline |
Definition at line 88 of file ctpl_stl.h.
88 {
return *this->threads[
i]; }
◆ n_idle() [1/2]
int ctpl::thread_pool::n_idle |
( |
| ) |
|
|
inline |
Definition at line 64 of file ctpl.h.
64 {
return this->nWaiting; }
◆ n_idle() [2/2]
int ctpl::thread_pool::n_idle |
( |
| ) |
|
|
inline |
Definition at line 87 of file ctpl_stl.h.
87 {
return this->nWaiting; }
◆ pop() [1/2]
std::function<void(int)> ctpl::thread_pool::pop |
( |
| ) |
|
|
inline |
Definition at line 106 of file ctpl.h.
107 std::function<void(int id)> * _f =
nullptr;
109 std::unique_ptr<std::function<void(int id)>> func(_f);
111 std::function<void(int)>
f;
◆ pop() [2/2]
std::function<void(int)> ctpl::thread_pool::pop |
( |
| ) |
|
|
inline |
Definition at line 129 of file ctpl_stl.h.
130 std::function<void(int id)> * _f =
nullptr;
132 std::unique_ptr<std::function<void(int id)>> func(_f);
133 std::function<void(int)>
f;
◆ push() [1/4]
template<typename F , typename... Rest>
auto ctpl::thread_pool::push |
( |
F && |
f, |
|
|
Rest &&... |
rest |
|
) |
| -> std::future<decltype(f(0, rest...))> |
|
inline |
Definition at line 152 of file ctpl.h.
153 auto pck = std::make_shared<std::packaged_task<decltype(
f(0, rest...))(int)>>(
154 std::bind(std::forward<F>(
f), std::placeholders::_1, std::forward<Rest>(rest)...)
157 auto _f =
new std::function<void(int id)>([pck](
int id) {
162 std::unique_lock<std::mutex> lock(this->mutex);
163 this->cv.notify_one();
165 return pck->get_future();
◆ push() [2/4]
template<typename F >
auto ctpl::thread_pool::push |
( |
F && |
f | ) |
-> std::future<decltype(f(0))> |
|
inline |
Definition at line 171 of file ctpl.h.
172 auto pck = std::make_shared<std::packaged_task<decltype(f(0))(int)>>(std::forward<F>(
f));
174 auto _f =
new std::function<void(int id)>([pck](
int id) {
179 std::unique_lock<std::mutex> lock(this->mutex);
180 this->cv.notify_one();
182 return pck->get_future();
◆ push() [3/4]
template<typename F , typename... Rest>
auto ctpl::thread_pool::push |
( |
F && |
f, |
|
|
Rest &&... |
rest |
|
) |
| -> std::future<decltype(f(0, rest...))> |
|
inline |
Definition at line 173 of file ctpl_stl.h.
174 auto pck = std::make_shared<std::packaged_task<decltype(
f(0, rest...))(int)>>(
175 std::bind(std::forward<F>(
f), std::placeholders::_1, std::forward<Rest>(rest)...)
177 auto _f =
new std::function<void(int id)>([pck](
int id) {
181 std::unique_lock<std::mutex> lock(this->mutex);
182 this->cv.notify_one();
183 return pck->get_future();
◆ push() [4/4]
template<typename F >
auto ctpl::thread_pool::push |
( |
F && |
f | ) |
-> std::future<decltype(f(0))> |
|
inline |
Definition at line 189 of file ctpl_stl.h.
190 auto pck = std::make_shared<std::packaged_task<decltype(f(0))(int)>>(std::forward<F>(
f));
191 auto _f =
new std::function<void(int id)>([pck](
int id) {
195 std::unique_lock<std::mutex> lock(this->mutex);
196 this->cv.notify_one();
197 return pck->get_future();
◆ resize() [1/2]
void ctpl::thread_pool::resize |
( |
int |
nThreads | ) |
|
|
inline |
Definition at line 70 of file ctpl.h.
71 if (!this->isStop && !this->isDone) {
72 int oldNThreads =
static_cast<int>(this->threads.size());
73 if (oldNThreads <= nThreads) {
74 this->threads.resize(nThreads);
75 this->flags.resize(nThreads);
77 for (
int i = oldNThreads;
i < nThreads; ++
i) {
78 this->flags[
i] = std::make_shared<std::atomic<bool>>(
false);
83 for (
int i = oldNThreads - 1;
i >= nThreads; --
i) {
84 *this->flags[
i] =
true;
85 this->threads[
i]->detach();
89 std::unique_lock<std::mutex> lock(this->mutex);
90 this->cv.notify_all();
92 this->threads.resize(nThreads);
93 this->flags.resize(nThreads);
◆ resize() [2/2]
void ctpl::thread_pool::resize |
( |
int |
nThreads | ) |
|
|
inline |
Definition at line 93 of file ctpl_stl.h.
94 if (!this->isStop && !this->isDone) {
95 int oldNThreads =
static_cast<int>(this->threads.size());
96 if (oldNThreads <= nThreads) {
97 this->threads.resize(nThreads);
98 this->flags.resize(nThreads);
100 for (
int i = oldNThreads;
i < nThreads; ++
i) {
101 this->flags[
i] = std::make_shared<std::atomic<bool>>(
false);
106 for (
int i = oldNThreads - 1;
i >= nThreads; --
i) {
107 *this->flags[
i] =
true;
108 this->threads[
i]->detach();
112 std::unique_lock<std::mutex> lock(this->mutex);
113 this->cv.notify_all();
115 this->threads.resize(nThreads);
116 this->flags.resize(nThreads);
◆ size() [1/2]
int ctpl::thread_pool::size |
( |
| ) |
|
|
inline |
Definition at line 61 of file ctpl.h.
61 {
return static_cast<int>(this->threads.size()); }
◆ size() [2/2]
int ctpl::thread_pool::size |
( |
| ) |
|
|
inline |
Definition at line 84 of file ctpl_stl.h.
84 {
return static_cast<int>(this->threads.size()); }
◆ stop() [1/2]
void ctpl::thread_pool::stop |
( |
bool |
isWait = false | ) |
|
|
inline |
Definition at line 121 of file ctpl.h.
126 for (
int i = 0,
n = this->
size();
i <
n; ++
i) {
127 *this->flags[
i] =
true;
132 if (this->isDone || this->isStop)
137 std::unique_lock<std::mutex> lock(this->mutex);
138 this->cv.notify_all();
140 for (
int i = 0; i < static_cast<int>(this->threads.size()); ++
i) {
141 if (this->threads[
i]->joinable())
142 this->threads[
i]->join();
147 this->threads.clear();
◆ stop() [2/2]
void ctpl::thread_pool::stop |
( |
bool |
isWait = false | ) |
|
|
inline |
Definition at line 142 of file ctpl_stl.h.
147 for (
int i = 0,
n = this->
size();
i <
n; ++
i) {
148 *this->flags[
i] =
true;
153 if (this->isDone || this->isStop)
158 std::unique_lock<std::mutex> lock(this->mutex);
159 this->cv.notify_all();
161 for (
int i = 0; i < static_cast<int>(this->threads.size()); ++
i) {
162 if (this->threads[
i]->joinable())
163 this->threads[
i]->join();
168 this->threads.clear();
The documentation for this class was generated from the following files: