Class ThreadPool
- Namespace
- Jitter2.Parallelization
- Assembly
- Jitter2.dll
Provides a persistent worker thread pool for parallel task execution within the physics engine.
public sealed class ThreadPool
- Inheritance
-
ThreadPool
- Inherited Members
Remarks
Worker threads are kept alive for the lifetime of the pool to avoid wake-up overhead. Tasks are distributed via per-thread queues with work stealing for load balancing.
The main thread participates as "worker 0" during Execute() calls. Use ResumeWorkers() and PauseWorkers() to control thread activity between simulation steps.
Fields
ThreadsPerProcessor
The fraction of available processors to use for worker threads.
public const float ThreadsPerProcessor = 0.9
Field Value
Properties
Instance
Implements the singleton pattern to provide a single instance of the ThreadPool.
public static ThreadPool Instance { get; }
Property Value
InstanceInitialized
Indicates whether the ThreadPool instance is initialized.
public static bool InstanceInitialized { get; }
Property Value
- bool
trueif initialized; otherwise,false.
ThreadCount
Gets the number of worker threads managed by this pool.
public int ThreadCount { get; }
Property Value
- int
Includes the main thread as worker 0.
ThreadCountSuggestion
Gets the suggested number of threads based on available processors.
public static int ThreadCountSuggestion { get; }
Property Value
- int
At least 1, computed as ThreadsPerProcessor × processor count.
Methods
AddTask<T>(Action<T>, T)
Adds a task to the queue for later execution.
public void AddTask<T>(Action<T> action, T parameter)
Parameters
actionAction<T>The action to execute.
parameterTThe parameter to pass to the action.
Type Parameters
TThe type of the task parameter.
Remarks
Tasks are not executed until Execute() is called. This method is not thread-safe and must be called from a single thread.
ChangeThreadCount(int)
Changes the number of worker threads.
public void ChangeThreadCount(int numThreads)
Parameters
numThreadsintThe new thread count.
Remarks
Existing worker threads are stopped and new ones are created. This operation blocks until all previous threads have terminated.
Execute()
Executes all queued tasks and blocks until completion.
public void Execute()
Remarks
Tasks are distributed to per-thread queues in round-robin order. The main thread participates as worker 0 and performs work stealing from other queues.
This method automatically calls ResumeWorkers() at the start.
PauseWorkers()
Pauses all worker threads after they finish their current tasks.
public void PauseWorkers()
Remarks
Workers will block on a wait handle until ResumeWorkers() is called. This reduces CPU usage between simulation steps.
ResumeWorkers()
Resumes all worker threads so they can process queued tasks.
public void ResumeWorkers()
Remarks
Called automatically by Execute(). Manual calls are typically only needed when using Persistent.