Skip to content

Commit

Permalink
chore: remove unecessary try/catch
Browse files Browse the repository at this point in the history
We suppress exceptions before enquing the task so it doesn't seem necessary to have try/catch when we invoke the enqueued task
  • Loading branch information
DeveloperPaul123 committed Jul 3, 2024
1 parent 2d07831 commit 7125885
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions include/thread_pool/thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,15 @@ namespace dp {
do {
// invoke the task
while (auto task = tasks_[id].tasks.pop_front()) {
try {
// decrement the unassigned tasks as the task is now going
// to be executed
unassigned_tasks_.fetch_sub(1, std::memory_order_release);
// invoke the task
std::invoke(std::move(task.value()));
// the above task can push more work onto the pool, so we
// only decrement the in flights once the task has been
// executed because now it's now longer "in flight"
in_flight_tasks_.fetch_sub(1, std::memory_order_release);
} catch (...) {
}
// decrement the unassigned tasks as the task is now going
// to be executed
unassigned_tasks_.fetch_sub(1, std::memory_order_release);
// invoke the task
std::invoke(std::move(task.value()));
// the above task can push more work onto the pool, so we
// only decrement the in flights once the task has been
// executed because now it's now longer "in flight"
in_flight_tasks_.fetch_sub(1, std::memory_order_release);
}

// try to steal a task
Expand Down Expand Up @@ -235,6 +232,7 @@ namespace dp {
// would only be a problem if there are zero threads
return;
}
// get the index
auto i = *(i_opt);
unassigned_tasks_.fetch_add(1, std::memory_order_release);
const auto prev_in_flight = in_flight_tasks_.fetch_add(1, std::memory_order_release);
Expand All @@ -244,6 +242,7 @@ namespace dp {
threads_complete_signal_.store(false, std::memory_order_release);
}

// assign work
tasks_[i].tasks.push_back(std::forward<Function>(f));
tasks_[i].signal.release();
}
Expand Down

0 comments on commit 7125885

Please sign in to comment.