CUGL 2.3
Cornell University Game Library
|
#include <CUThreadPool.h>
Public Member Functions | |
ThreadPool () | |
~ThreadPool () | |
void | dispose () |
virtual bool | init (int threads=4) |
void | addTask (const std::function< void()> &task) |
void | stop () |
bool | isStopped () const |
bool | isShutdown () const |
Static Public Member Functions | |
static std::shared_ptr< ThreadPool > | alloc (int threads=4) |
Class to providing a collection of worker threads.
This is a general purpose class for performing tasks asynchronously. There is no notification process for when a task is complete. Instead, your task should either set a flag, or execute a callback when it is done.
There are some important safety considerations for using this class over direct thread objects. For example, stopping a thread pool does not shut it down immediately; it just marks it for shutdown. Because of mutex locks, it is not safe to delete a thread pool until it is completely shutdown.
More importantly, we do not allow for detached threads. This makes no sense in this application, because the threads share a resource (_taskQueue) with the main thread that will be deleted. It is therefore unsafe for the threads to ever detach.
See the class AssetManager
for an example of how to use a thread pool.
|
inline |
Creates a thread pool with no active threads.
You must initialize this thread pool before use.
NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate a thread pool on the heap, use one of the static constructors instead.
|
inline |
Deletes this thread pool, destroying all resources.
It is a bad idea to destroy the thread pool if the pool is not yet shut down. The task queue is shared by the child threads, so we cannot delete it until all the threads complete. This destructor will block unti showndown.
void cugl::ThreadPool::addTask | ( | const std::function< void()> & | task | ) |
Adds a task to the thread pool.
A task is a void returning function with no parameters. If you need state in the task, you should use a method call for the state. The task will not be executed immediately, but must wait for the first available worker.
task | the task function to add to the thread pool |
|
inlinestatic |
Returns a newly allocated thread pool with the given number of threads.
You can specify the number of simultaneous worker threads. We find that 4 is generally a good number, even if you have a lot of tasks. Much more than the number of cores on a machine is counter-productive.
threads | the number of threads in this pool |
void cugl::ThreadPool::dispose | ( | ) |
Disposes this thread pool, releasing all memory.
A disposed thread pool can be safely reinitialized. However, it is a bad idea to destroy the thread pool if the pool is not yet shut down. The task queue is shared by the child threads, so we cannot delete it until all the threads complete. This destructor will block unti showndown.
|
virtual |
Initializes a thread pool with the given number of threads.
You can specify the number of simultaneous worker threads. We find that 4 is generally a good number, even if you have a lot of tasks. Much more than the number of cores on a machine is counter-productive.
threads | the number of threads in this pool |
|
inline |
Returns whether the thread pool has been shut down.
A shut down thread pool has no active threads and is safe for deletion.
|
inline |
Returns whether the thread pool has been stopped.
A stopped thread pool is marked for shutdown, but it shutdown has not necessarily completed. Shutdown will be complete when the current child threads have finished with their tasks.
void cugl::ThreadPool::stop | ( | ) |
Stop the thread pool, marking it for shut down.
A stopped thread pool is marked for shutdown, but it shutdown has not necessarily completed. Shutdown will be complete when the current child threads have finished with their tasks.