Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/ex/thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,10 @@ class thread_pool::impl
{
std::lock_guard<std::mutex> lock(mutex_);
push(&c);
// Under the lock so the pool cannot drain, join, and
// destroy the condition variable mid-signal.
work_cv_.notify_one();
}
work_cv_.notify_one();
}

void
Expand Down
32 changes: 32 additions & 0 deletions test/unit/ex/thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <boost/capy/concept/execution_context.hpp>
#include <boost/capy/concept/executor.hpp>
#include <boost/capy/ex/run.hpp>
#include <boost/capy/ex/run_async.hpp>
#include <boost/capy/ex/work_guard.hpp>

Expand Down Expand Up @@ -697,6 +698,36 @@ struct thread_pool_test
BOOST_TEST_EQ(result.load(), 42);
}

static task<void>
hop_and_count(
thread_pool::executor_type worker_ex, std::atomic<int>& count)
{
count += co_await boost::capy::run(worker_ex)(returns_int(1));
}

void
testForeignPostAfterJoin()
{
// The worker pool's thread posts the continuation back into a
// pool that joins and dies immediately after the work drains,
// exercising the window where the poster is still inside
// post() during destruction.
thread_pool worker(1);
auto worker_ex = worker.get_executor();
std::atomic<int> count{0};

for(int i = 0; i < 50; ++i)
{
thread_pool pool(1);
run_async(pool.get_executor())(
hop_and_count(worker_ex, count));
pool.join();
}

BOOST_TEST_EQ(count.load(), 50);
worker.join();
}

void
run()
{
Expand Down Expand Up @@ -726,6 +757,7 @@ struct thread_pool_test
testStopCallbackRepeated();
testWorkGuardKeepsPoolAlive();
testJoinWithRunAsync();
testForeignPostAfterJoin();
}
};

Expand Down
Loading