Cornell Cocos
Cornell Extensions to Cocos2d
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
ThreadPool Class Reference

#include <CUThreadPool.h>

Inheritance diagram for ThreadPool:

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 ThreadPoolcreate (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
 

Detailed Description

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.

Constructor & Destructor Documentation

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.

Member Function Documentation

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.

Parameters
taskthe task function to add to the thread pool
NS_CC_BEGIN ThreadPool * ThreadPool::create ( int  threads = 4)
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.

Parameters
threadsthe number of threads in this pool
Returns
An autoreleased thread pool
bool ThreadPool::init ( int  threads = 4)
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.

Parameters
threadsthe number of threads in this pool
Returns
true if the obstacle is initialized properly, false otherwise.
bool ThreadPool::isShutdown ( ) const
inline

Returns whether the thread pool has been shut down.

A shut down thread pool has no active threads and is safe for deletion.

Returns
whether the thread pool has been shut down.
bool ThreadPool::isStopped ( ) const
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.

Returns
whether the thread pool has been stopped.
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.

void ThreadPool::threadFunc ( )
protected

The body function of a single thread; it pulls tasks from the task queue

Member Data Documentation

int ThreadPool::_complete
protected

The number of child threads that are completed

bool ThreadPool::_detach
protected

Whether or not to detach the threads

std::mutex ThreadPool::_queueMutex
protected

A mutex lock for the task queue

bool ThreadPool::_stop
protected

Whether or not the thread pool has been marked for shutdown

std::condition_variable ThreadPool::_taskCondition
protected

A condition variable to manage tasks waiting for a worker

std::queue< std::function<void()> > ThreadPool::_taskQueue
protected

Tasks waiting to be assigned to a thread

std::vector<std::thread> ThreadPool::_workers
protected

The individual worker threads for this thread pool


The documentation for this class was generated from the following files: