Skip to content

Can this thread pool recursively add tasks? #72

Answered by DeveloperPaul123
etorth asked this question in Q&A
Discussion options

You must be logged in to vote

In short, yes:

void recursive_sequential_sum(std::atomic_int32_t& counter, int count, dp::thread_pool<>& pool) {
counter.fetch_add(count);
if (count > 1) {
pool.enqueue_detach(recursive_sequential_sum, std::ref(counter), count - 1, std::ref(pool));
}
}
TEST_CASE("Recursive enqueue calls work correctly") {
std::atomic_int32_t counter = 0;
constexpr auto start = 1000;
{
dp::thread_pool pool(4);
recursive_sequential_sum(counter, start, pool);
}
auto expected_sum = 0;
for (int i = 0; i <= start; i++) {
expected_sum += i;
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by DeveloperPaul123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #71 on August 29, 2024 13:30.