ThreadPool

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.

Constructors

this
this(size_t workers, size_t q_size)

Create a new ThreadPool.

Members

Aliases

JobD
alias JobD = void delegate(Args)

An alias for the type of delegates this thread pool considers a job

Functions

activeJobs
size_t activeJobs()

Get the number of jobs being executed

append
void append(JobD job, Args args)

Put a job into the pool for eventual execution.

assign
void assign(JobD job, Args args)

Assign the given job to a thread immediately or block until one is available

finish
void finish()

Complete all pending jobs and shutdown.

pendingJobs
size_t pendingJobs()

Get the number of jobs waiting to be executed

shutdown
void shutdown()

Finish currently executing jobs and drop all pending.

tryAssign
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)

wait
void wait()

Block until all pending jobs complete, but do not shut down. This allows more tasks to be added later.

Examples

// 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.

Meta