Class ThreadPool
- Namespace
- Jitter2.Parallelization
- Assembly
- Jitter2.dll
Runs queued tasks on reusable worker threads and the calling thread.
public sealed class ThreadPool
- Inheritance
-
ThreadPool
- Inherited Members
Remarks
Add tasks with AddTask<T>(Action<T>, T), then call Execute() to run them and wait for completion. Do not queue tasks, execute them, or change the thread count concurrently.
Call PauseWorkers() to reduce CPU usage between executions. Execute() resumes workers automatically.
Fields
ThreadsPerProcessor
The fraction of available processors to use for worker threads.
public const float ThreadsPerProcessor = 0.9
Field Value
Properties
Instance
Gets the shared ThreadPool instance.
public static ThreadPool Instance { get; }
Property Value
Remarks
Do not access this property concurrently before the pool has been initialized.
InstanceInitialized
Indicates whether the ThreadPool instance is initialized.
public static bool InstanceInitialized { get; }
Property Value
- bool
trueif initialized; otherwise,false.
IsPaused
Gets whether worker threads have been requested to pause when idle.
public bool IsPaused { get; }
Property Value
Remarks
A value of true does not guarantee that every worker is already blocked; workers may still be finishing previously queued work.
ThreadCount
Gets the total number of threads that can process tasks, including the calling thread.
public int ThreadCount { get; }
Property Value
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.
Exceptions
- ArgumentNullException
Thrown when
actionis null.
ChangeThreadCount(int)
Changes the total thread count, including the calling thread.
public void ChangeThreadCount(int numThreads)
Parameters
numThreadsintThe new thread count. Use 1 to process tasks only on the calling thread.
Remarks
This method waits for the previous worker threads to stop before creating replacements.
Exceptions
- ArgumentOutOfRangeException
Thrown when
numThreadsis less than one.
Execute()
Executes all queued tasks and blocks until completion.
public void Execute()
Remarks
The calling thread also processes tasks. Workers are resumed automatically. If a task throws an exception, all tasks finish before the exception is rethrown here.
PauseWorkers()
Pauses all worker threads after they finish their current tasks.
public void PauseWorkers()
Remarks
Workers wait 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() and by the world during multithreaded steps. Manual calls are useful when managing the pool directly.