Skip to content
ikopylov edited this page Dec 26, 2014 · 2 revisions

Qoollo thread pool

Thread pool is the container of threads that can perform a number of tasks placed to the queue by user.

The library contains 3 implementations of thread pools:

  1. StaticThreadPool - thread pool where the number of threads is given by user. This thread pool is ideal for the case when you can predict the workload and calculate the optimal number of threads.
  2. DynamicThreadPool - thread pool that automatically adjust the number of threads to increase the throughput.
  3. SystemThreadPool - simple wrapper around system thread pool.

It is important to note that these thread pools are not guaranty to execute tasks in FIFO manner.

The main advantage of these custom thread pools is that they make it possible to isolate execution of some group of tasks so that they will not interfere with other code. You just create a separate instance of thread pool for that group of tasks.

Usage sample:

// Create the thread pool
var threadPool = new DynamicThreadPool(
    minThreadCount: 0, maxThreadCount: 4 * Environment.ProcessorCount, 
    queueBoundedCapacity: 4096, 
    name: "name");

// Run simple tasks
for (int i = 0; i < 10; i++)
    threadPool.Run((val) => Console.WriteLine(val), i);

// Run task and get associated System.Threading.Tasks.Task
var task = threadPool.RunAsTask(() => Console.WriteLine("run as task"));
task.Wait();

// Dispose our thread pool
threadPool.Dispose(waitForStop: true, letFinishProcess: true, completeAdding: true);
Clone this wiki locally