Motivation
ThreadPool.JobQueue currently mixes queue storage and synchronization:
pushBatch acquires the queue mutex internally.
pop assumes the caller already holds the mutex.
workerLoop and deinit access the queue’s mutex and condition directly.
pushBatch receives the ThreadPool to inspect its shutdown state.
There is no known data race because current pop calls hold the mutex, but the locking contract is split
across JobQueue and ThreadPool. The queue is also not independently thread-safe as its comment suggests.
Proposed change
- Keep the work queue as a plain, non-thread-safe data structure.
- Move the mutex, condition variable, sleeping-worker count, and shutdown coordination to
ThreadPool.
- Make
ThreadPool responsible for locking around all queue operations.
- Guard the queue state and shutdown predicate with the same mutex.
The queue representation can remain unchanged or be replaced separately. The main goal is to make
synchronization ownership explicit and centralized in ThreadPool such as std.
Motivation
ThreadPool.JobQueuecurrently mixes queue storage and synchronization:pushBatchacquires the queue mutex internally.popassumes the caller already holds the mutex.workerLoopanddeinitaccess the queue’s mutex and condition directly.pushBatchreceives theThreadPoolto inspect its shutdown state.There is no known data race because current
popcalls hold the mutex, but the locking contract is splitacross
JobQueueandThreadPool. The queue is also not independently thread-safe as its comment suggests.Proposed change
ThreadPool.ThreadPoolresponsible for locking around all queue operations.The queue representation can remain unchanged or be replaced separately. The main goal is to make
synchronization ownership explicit and centralized in
ThreadPoolsuch as std.