Skip to content

Commit 88dee98

Browse files
author
tnagler
committed
pull tpool
1 parent 2c97bb9 commit 88dee98

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

inst/include/RcppThread/ThreadPool.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ inline ThreadPool::ThreadPool(size_t nWorkers)
8888
// use inner while to save a few cash misses calling done()
8989
while (taskManager_.try_pop(task, id))
9090
execute(task);
91-
} while (!todoList_.done());
91+
} while (!todoList_.empty());
9292
}
9393
});
9494
}
@@ -231,7 +231,7 @@ ThreadPool::parallelForEach(I& items, F&& f, size_t nBatches)
231231
inline void
232232
ThreadPool::wait()
233233
{
234-
while (!todoList_.done()) {
234+
while (!todoList_.empty()) {
235235
todoList_.wait(50);
236236
Rcout << "";
237237
checkUserInterrupt();

inst/include/RcppThread/tpool.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@
3030
//! tpool namespace
3131
namespace tpool {
3232

33+
//! @brief Todo list - a synchronization primitive.
34+
//! @details Add a task with `add()`, cross it off with `cross()`, and wait for
35+
//! all tasks to complete with `wait()`.
3336
class TodoList
3437
{
3538
public:
3639
//! constructs the todo list.
3740
//! @param num_tasks initial number of tasks.
3841
TodoList(size_t num_tasks = 0) noexcept
39-
: num_tasks_{ static_cast<int>(num_tasks) }
42+
: num_tasks_(num_tasks)
4043
{}
4144

4245
//! adds tasks to the list.
@@ -49,12 +52,15 @@ class TodoList
4952
{
5053
num_tasks_.fetch_sub(num_tasks);
5154
if (num_tasks_ <= 0) {
52-
std::lock_guard<std::mutex> lk(mtx_);
55+
{
56+
std::lock_guard<std::mutex> lk(mtx_); // must lock before signal
57+
}
5358
cv_.notify_all();
5459
}
5560
}
5661

57-
bool done() const noexcept { return num_tasks_ <= 0; }
62+
//! checks whether list is empty.
63+
bool empty() const noexcept { return num_tasks_ == 0; }
5864

5965
//! waits for the list to be empty.
6066
//! @param millis if > 0; waiting aborts after waiting that many

0 commit comments

Comments
 (0)