-
Notifications
You must be signed in to change notification settings - Fork 6
Thread Pool Sizing
The thread count adjusts dynamically based on the workload experienced by the threads in the pool.
Maximum number of threads can be setted through PowerPoolOption.MaxThreads
(the default value is the number of processor cores * 2), at which point the number of threads maintained by the thread pool will stabilize at this count, unless the DestroyThreadOption property is set.
// MaxThreads == the number of processor cores * 2
PowerPool powerPool = new PowerPool();
// MaxThreads == 8
PowerPool powerPool = new PowerPool(new PowerPoolOption()
{
MaxThreads = 8
});
After setting the DestroyThreadOption
, the initial number of threads in the thread pool will be set to DestroyThreadOption.MinThreads
. When all threads are busy and a new work is added, new threads will be created until the number reaches PowerPoolOption.MaxThreads
.
When a thread becomes idle, it will remain idle and wait for new works until the wait time exceeds DestroyThreadOption.KeepAliveTime
. After this time, the thread will be released unless the current number of threads is equal to DestroyThreadOption.MinThreads
.
PowerPool powerPool = new PowerPool(new PowerPoolOption()
{
MaxThreads = 8,
DestroyThreadOption = new DestroyThreadOption() { MinThreads = 4, KeepAliveTime = 3000 }
});
If there are long-running works (i.e., works where WorkOption.LongRunning
is true), the maximum number of threads in the thread pool will be MaxThreads plus the number of long-running works. This design specifically addresses thread starvation by allowing long-running works to operate on additional threads, ensuring that short-term works retain access to the thread pool and can be processed without undue delay.
// Thread count == 8
PowerPool powerPool = new PowerPool(new PowerPoolOption()
{
MaxThreads = 8
});
// Thread count == 9
powerPool.QueueWorkItem(() =>
{
// Do something
}, new WorkOption()
{
LongRunning = true,
});
- Pool Control | Work Control
- Thread Pool Sizing
- Work Callback | Default Callback
- Parallel Execution
- Work Priority | Thread Priority
- Error Handling
- Work Timeout | Cumulative Work Timeout
- Work Dependency
- Work Group
- Events
- Runtime Status
- Running Timer
- Queue Type (FIFO | LIFO | Custom)
- Load Balancing
- Lock-Free
Core
Results
Options