Document _tl_should_exit protocol in worker loops

The _tl_should_exit mechanism in start_routine (and
start_routine_autoscale) is correct but the control flow is non-obvious:
_remove_thread_scaling_locked enqueues a lambda that sets the
thread-local flag and calls _remove_self (detach + erase), then the
worker checks the flag after running the func and exits.

Replace the terse one-line comments with an explanation of the full
protocol so future readers don't mistake it for dead code.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Antonio S. Musumeci 2026-04-09 23:40:47 -04:00
parent 2333f6078a
commit 96824eab31

View file

@ -375,7 +375,11 @@ ThreadPool::start_routine(void *arg_)
btp->_tasks_completed.fetch_add(1,std::memory_order_relaxed);
// Check if _remove_thread_scaling_locked told us to exit
// _remove_thread_scaling_locked enqueues a lambda that sets
// _tl_should_exit and calls _remove_self (which detaches +
// erases from _threads). The lambda runs as normal work above,
// so we check the flag here. The thread has already been
// detached by _remove_self, so we just return.
if(_tl_should_exit)
{
_tl_should_exit = false;
@ -459,7 +463,7 @@ ThreadPool::start_routine_autoscale(void *arg_)
btp->_tasks_completed.fetch_add(1,std::memory_order_relaxed);
// Check if _remove_thread_scaling_locked told us to exit
// See comment in start_routine for the _tl_should_exit protocol.
if(_tl_should_exit)
{
_tl_should_exit = false;