Drain _remove_count in destructor before sending poison pills

If remove_thread() was called but no worker claimed the decrement
before shutdown, _remove_count remains positive.  Workers exiting
via poison pills would enter the _remove_count CAS loop in
_process_func, but since _stop is set, _remove_self is a no-op
anyway.  The stale counter is harmless at runtime but confusing
when debugging — it suggests pending work that will never complete.

Zero the counter after _stop is set and before enqueuing poison
pills so the shutdown path is clean and unambiguous.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Antonio S. Musumeci 2026-04-10 11:46:59 -04:00
parent b9552758fd
commit 80af7ffcc0

View file

@ -317,6 +317,11 @@ ThreadPool::~ThreadPool()
_name.c_str(),
snapshot.size());
// Drain any pending _remove_count so workers exiting via poison
// pills don't also try to detach themselves (which would be a
// no-op due to _stop, but avoids confusion in debugging).
_remove_count.store(0,std::memory_order_relaxed);
// Enqueue one poison pill per thread
for(std::size_t i = 0; i < snapshot.size(); ++i)
_queue.enqueue_unbounded(Func{});