Create a new ThreadPool.
An alias for the type of delegates this thread pool considers a job
Get the number of jobs being executed
Put a job into the pool for eventual execution.
Assign the given job to a thread immediately or block until one is available
Complete all pending jobs and shutdown.
Get the number of jobs waiting to be executed
Finish currently executing jobs and drop all pending.
Assign the given job to a thread immediately or return false if none is available. (Returns true if one was available)
Block until all pending jobs complete, but do not shut down. This allows more tasks to be added later.
// 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.
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.