#include <mcllib/MCThreadPool.h>
Inheritance diagram for MCThreadPool:
Public Member Functions | |
mcuint32 | addJob (MCThreadJob *pJob) |
Add a job to the pool. | |
void | join () |
Wait until all the jobs have finished running. | |
MCThreadPool (const MCString &name, mcuint32 maxConcurrency=1, MCThread::EType type=MCThread::THRTYPEUSER, MCThread::EScope scope=MCThread::THRSCOPELOCAL, MCThread::EState state=MCThread::THRSTATEJOIN, MCThread::EPriority pri=MCThread::THRPRINORMAL, mcstacksize_t sz=0) | |
Construct a named thread pool object. | |
void | removeJob (mcuint32 jobId) |
Remove a job from the pool. | |
void | start () |
Start the jobs in the pool running. | |
void | stop () |
Stop the jobs in the pool running. |
The MCThreadPool class is responsible for allocating threads to the different thread jobs (see MCThreadJob) that it is managing. Threads are created on demand and allocated to jobs to ensure that each job can keep up with the work it is doing.
The thread pool has jobs added to it with the addJob() method. Once all the necessary jobs have been added, the pool is set running with the start() method. To wait for all jobs in the pool to complete, the join() method is called. To prematurely stop all jobs call the stop() method.
Example.
// Allow up to 20 threads in a pool called "MyPool" MCThreadPool pool("MyPool", 20); // Add three jobs to the pool pool.addJob(...); pool.addJob(...); pool.addJob(...); // Start the pool scheduling threads for the three jobs pool.start(); // Wait for the jobs to complete pool.join();If the thread pool is destructed the stop() method is called automatically.
|
Construct a named thread pool object. The type and subsequent parameters are used when threads need to be created. It is possible to have different pools with different thread parameters (e.g. low priority for background threads, high priority for interactive or low-latency threads).
|
|
Add a job to the pool. addJob() cannot be called once the pool has been start()ed.
|
|
Wait until all the jobs have finished running. Returns immediately if the pool is not running. |
|
Remove a job from the pool. removeJob() cannot be called once the pool has been start()ed.
|
|
Start the jobs in the pool running. No threads will be created until start is called. Once start is called, enough threads will be created to satisfy the minimum concurrency requirements of all the jobs in the pool. Additional threads may be automatically created if the pool determines that this is required. Only the stop() or join() methods may be called after start(). Start is ignored if the pool is already running. |
|
Stop the jobs in the pool running. Stop is ignored if the pool is already stopped. Stop will end all jobs in the pool whether or not they have finished running. Stop will return once all threads managed by the pool have finished. |