tango.core.ThreadPool

This module provides an implementation of the classical thread-pool model.

License:

BSD style: see license.txt

Author:

Anders Halager
class ThreadPool(Args...) #
A thread pool is a way to process multiple jobs in parallel without creating a new thread for each job. This way the overhead of creating a thread is only paid once, and not once for each job and you can limit the maximum number of threads active at any one point.
In this case a "job" is simply a delegate and some parameters the delegate will be called with after having been added to the thread pool's queue.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// create a new pool with two threads
auto pool = new ThreadPool!(int)(2);
void delegate(int) f = (int x) { Log(x); };

// Now we have three ways of telling the pool to execute our jobs
// First we can say we just want it done at some later point
pool.append(f, 1);
// Secondly we can ask for a job to be done as soon as possible, blocking
// until it is started by some thread
pool.assign(f, 2);
// Finally we can say we either want it done immediately or not at all
if (pool.tryAssign(f, 3))
    Log("Someone took the job!");
else
    Log("No one was available to do the job right now");
// After giving the pool some jobs to do, we need to give it a chance to
// finish, so we can do one of two things.
// Choice no. 1 is to finish what has already been assigned to the threads,
// but ignore any remaining queued jobs
//   pool.shutdown();
// The other choice is to finish all jobs currently executing or in queue:
pool.finish();

If append isn't called there should be no additional heap allocations after initialization.

alias void delegate(Args) JobD #
An alias for the type of delegates this thread pool considers a job
this(size_t workers, size_t q_size = 0) #
Create a new ThreadPool.

Params:

workersThe amount of threads to spawn
q_sizeThe expected size of the queue (how many elements are preallocated)
void assign(JobD job, Args args) #
Assign the given job to a thread immediately or block until one is available
bool tryAssign(JobD job, Args args) #
Assign the given job to a thread immediately or return false if none is available. (Returns true if one was available)
void append(JobD job, Args args) #
Put a job into the pool for eventual execution.

Warning:

Acts as a stack, not a queue as you would expect
size_t pendingJobs() #
Get the number of jobs waiting to be executed
size_t activeJobs() #
Get the number of jobs being executed
void shutdown() #
Finish currently executing jobs and drop all pending.
void finish() #
Complete all pending jobs and shutdown.