Skip to content

Commit

Permalink
chore: rename counters to make intention more clear
Browse files Browse the repository at this point in the history
I re-named the counters to names I feel make their use more clear (and distinct). This helps make clear (in my opinion) why there are 2 counters instead of just one as GabTux noted in the comment on #62
  • Loading branch information
DeveloperPaul123 committed Apr 25, 2024
1 parent 8814340 commit b79d180
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions include/thread_pool/thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ namespace dp {
// invoke the task
while (auto task = tasks_[id].tasks.pop_front()) {
try {
pending_tasks_.fetch_sub(1, std::memory_order_release);
unassigned_tasks_.fetch_sub(1, std::memory_order_release);
std::invoke(std::move(task.value()));
total_tasks_.fetch_sub(1, std::memory_order_release);
completed_tasks_.fetch_sub(1, std::memory_order_release);
} catch (...) {
}
}
Expand All @@ -62,19 +62,21 @@ namespace dp {
const std::size_t index = (id + j) % tasks_.size();
if (auto task = tasks_[index].tasks.steal()) {
// steal a task
pending_tasks_.fetch_sub(1, std::memory_order_release);
unassigned_tasks_.fetch_sub(1, std::memory_order_release);
std::invoke(std::move(task.value()));
total_tasks_.fetch_sub(1, std::memory_order_release);
completed_tasks_.fetch_sub(1, std::memory_order_release);
// stop stealing once we have invoked a stolen task
break;
}
}

} while (pending_tasks_.load(std::memory_order_acquire) > 0);
// check if there are any unassigned tasks before rotating to the
// front and waiting for more work
} while (unassigned_tasks_.load(std::memory_order_acquire) > 0);

priority_queue_.rotate_to_front(id);

if (total_tasks_.load(std::memory_order_acquire) == 0) {
// check if all tasks are completed and release the barrier (binary
// semaphore)
if (completed_tasks_.load(std::memory_order_acquire) == 0) {
threads_done_.release();
}

Expand All @@ -96,7 +98,7 @@ namespace dp {
}

~thread_pool() {
if (total_tasks_.load(std::memory_order_acquire) > 0) {
if (completed_tasks_.load(std::memory_order_acquire) > 0) {
// wait for all tasks to finish
threads_done_.acquire();
}
Expand Down Expand Up @@ -214,8 +216,8 @@ namespace dp {
return;
}
auto i = *(i_opt);
pending_tasks_.fetch_add(1, std::memory_order_relaxed);
total_tasks_.fetch_add(1, std::memory_order_relaxed);
unassigned_tasks_.fetch_add(1, std::memory_order_relaxed);
completed_tasks_.fetch_add(1, std::memory_order_relaxed);
tasks_[i].tasks.push_back(std::forward<Function>(f));
tasks_[i].signal.release();
}
Expand All @@ -228,7 +230,7 @@ namespace dp {
std::vector<ThreadType> threads_;
std::deque<task_item> tasks_;
dp::thread_safe_queue<std::size_t> priority_queue_;
std::atomic_int_fast64_t pending_tasks_{}, total_tasks_{};
std::atomic_int_fast64_t unassigned_tasks_{}, completed_tasks_{};
std::binary_semaphore threads_done_{0};
};

Expand Down

0 comments on commit b79d180

Please sign in to comment.