Skip to content

Thread pool: spawn threads in dispatch() - #1786

Merged
Mysticial merged 1 commit into
mainfrom
threadpool-default-spawn
Oct 31, 2026
Merged

Mysticial merged 1 commit into
mainfrom
threadpool-default-spawn

Conversation

@pa-devbot

Copy link
Copy Markdown
Collaborator

Fixes ThreadPool_Default::dispatch() never growing the thread pool.

The bug

dispatch() enqueued the task and called m_thread_cv.notify_one(), but never called spawn_threads(). Since all of the global pools in GlobalThreadPools.cpp start with zero threads, a plain dispatch() on a cold pool had no waiter to wake - the task sat in the queue and never ran. On a pool with fewer live threads than its maximum, the pool also never used its headroom.

dispatch_now_blocking(), try_dispatch_now() and run_in_parallel() all call spawn_threads(), so only the plain dispatch() path was affected.

The fix

Call spawn_threads() after the task is enqueued (under the lock), the same as the other entry points.

Testing

Standalone functional test against ThreadPool_Default:

  • a plain dispatch() on a 0-thread pool now runs (previously it hung forever),
  • 4 concurrent 300 ms tasks on a 0-thread pool (max 4) overlap - 301 ms wall time, peak concurrency 4 (previously serialized),
  • 8 tasks on a max-4 pool stay capped at 4 concurrent and run as two waves,
  • dispatch_now_blocking() / try_dispatch_now() still work.

pa-build + pa-test green.

dispatch() enqueued the task and called notify_one(), but never called
spawn_threads(). Since all of the global pools start with zero threads,
a plain dispatch() on a cold pool woke no one and the task never ran.
On a pool with fewer threads than its maximum, the pool also never
grew to use the headroom.

dispatch_now_blocking(), try_dispatch_now() and run_in_parallel() all
call spawn_threads(), so only the plain dispatch() path was affected.

Call spawn_threads() after the task is enqueued, the same as the other
entry points.
@Mysticial
Mysticial merged commit 43f2ede into main Oct 31, 2026
7 checks passed
@Mysticial
Mysticial deleted the threadpool-default-spawn branch October 31, 2026 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants