7
7
#pragma once
8
8
9
9
#include " RcppThread/RMonitor.hpp"
10
- #include " RcppThread/Rcout.hpp"
11
10
#include " RcppThread/Rcerr.hpp"
11
+ #include " RcppThread/Rcout.hpp"
12
12
#include " RcppThread/quickpool.hpp"
13
13
14
14
#include < atomic>
@@ -70,22 +70,21 @@ class ThreadPool
70
70
// ! constructs a thread pool with as many workers as there are cores.
71
71
inline ThreadPool::ThreadPool ()
72
72
: ThreadPool(std::thread::hardware_concurrency())
73
- {}
73
+ {
74
+ }
74
75
75
76
// ! constructs a thread pool with `nWorkers` threads.
76
77
// ! @param nWorkers number of worker threads to create; if `nWorkers = 0`, all
77
78
// ! work pushed to the pool will be done in the main thread.
78
79
inline ThreadPool::ThreadPool (size_t nWorkers)
79
80
: pool_{ new quickpool::ThreadPool (nWorkers) }
80
81
, owner_thread_{ std::this_thread::get_id () }
81
- {}
82
-
83
- // ! destructor joins all threads if possible.
84
- inline ThreadPool::~ThreadPool () noexcept
85
82
{
86
- this ->wait ();
87
83
}
88
84
85
+ // ! destructor joins all threads if possible.
86
+ inline ThreadPool::~ThreadPool () noexcept {}
87
+
89
88
// ! Access to the global thread pool instance.
90
89
inline ThreadPool&
91
90
ThreadPool::globalInstance ()
@@ -173,7 +172,8 @@ ThreadPool::parallelFor(int begin, int end, F f, size_t nBatches)
173
172
if (nBatches == 0 ) {
174
173
// each worker has its dedicated range, but can steal part of another
175
174
// worker's ranges when done with own
176
- auto thr = std::max (pool_->get_active_threads (), static_cast <size_t >(1 ));
175
+ auto thr =
176
+ std::max (pool_->get_active_threads (), static_cast <size_t >(1 ));
177
177
auto workers = quickpool::loop::create_workers<F>(f, begin, end, thr);
178
178
for (size_t k = 0 ; k < thr; k++) {
179
179
this ->push ([=] { workers->at (k).run (workers); });
@@ -189,7 +189,10 @@ ThreadPool::parallelFor(int begin, int end, F f, size_t nBatches)
189
189
int rem = nTasks % nBatches;
190
190
for (size_t b = 0 ; b < nBatches; b++) {
191
191
int bs = sz + (rem-- > 0 );
192
- this ->push ([=] { for (int i = begin; i < begin + bs; ++i) f (i); });
192
+ this ->push ([=] {
193
+ for (int i = begin; i < begin + bs; ++i)
194
+ f (i);
195
+ });
193
196
begin += bs;
194
197
}
195
198
}
0 commit comments