Cornell Cocos
Cornell Extensions to Cocos2d
|
#include <CUThreadPool.h>
Public Member Functions | |
void | addTask (const std::function< void()> &task) |
void | stop () |
bool | isStopped () const |
bool | isShutdown () const |
~ThreadPool () | |
virtual bool | init (int threads=4) |
Static Public Member Functions | |
static ThreadPool * | create (int threads=4) |
Public Attributes | |
CC_CONSTRUCTOR_ACCESS | __pad0__: ThreadPool() |
Protected Member Functions | |
void | threadFunc () |
Protected Attributes | |
std::vector< std::thread > | _workers |
std::queue< std::function< void()> > | _taskQueue |
std::mutex | _queueMutex |
std::condition_variable | _taskCondition |
bool | _detach |
bool | _stop |
int | _complete |
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.
This code is largely taken from the Cocos2d file AudioEngine.cpp, from the code for asynchronous asset loading. We have made some safety changes. In particular, 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 no longer 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. I have no idea who thought this was a good idea in AudioEngine.cpp.
See the class CUAssetManager for an example of how to use a thread pool.
ThreadPool::~ThreadPool | ( | ) |
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 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 |
|
static |
Creates a thread pool with the given number of threads.
You can specify the number of simultaneous worker threads. 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 |
|
virtual |
Initializes a thread pool with the given number of threads.
You can specify the number of simultaneous worker threads. 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 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.
|
protected |
The body function of a single thread; it pulls tasks from the task queue
|
protected |
The number of child threads that are completed
|
protected |
Whether or not to detach the threads
|
protected |
A mutex lock for the task queue
|
protected |
Whether or not the thread pool has been marked for shutdown
|
protected |
A condition variable to manage tasks waiting for a worker
|
protected |
Tasks waiting to be assigned to a thread
|
protected |
The individual worker threads for this thread pool