|
degate 0.1.1
|
#include <ThreadPool.h>
Public Member Functions | |
| ThreadPool (unsigned int n=4) | |
| ~ThreadPool () | |
| void | add (FunctionType f) |
| void | wait () |
Private Types | |
| typedef std::tr1::shared_ptr < boost::thread > | thread_shptr |
Private Member Functions | |
| void | spawn () |
Private Attributes | |
| unsigned int | max_n |
| std::list< FunctionType > | task_queue |
| std::list< thread_shptr > | running |
Definition at line 12 of file ThreadPool.h.
typedef std::tr1::shared_ptr<boost::thread> ThreadPool< FunctionType >::thread_shptr [private] |
Definition at line 14 of file ThreadPool.h.
| ThreadPool< FunctionType >::ThreadPool | ( | unsigned int | n = 4 | ) | [inline] |
Definition at line 35 of file ThreadPool.h.
: max_n(n) { }
| ThreadPool< FunctionType >::~ThreadPool | ( | ) | [inline] |
Definition at line 38 of file ThreadPool.h.
References ThreadPool< FunctionType >::wait().
{
wait();
}

| void ThreadPool< FunctionType >::add | ( | FunctionType | f | ) | [inline] |
Definition at line 43 of file ThreadPool.h.
References ThreadPool< FunctionType >::task_queue.
{
task_queue.push_back(f);
}
| void ThreadPool< FunctionType >::spawn | ( | ) | [inline, private] |
Definition at line 22 of file ThreadPool.h.
References ThreadPool< FunctionType >::max_n, ThreadPool< FunctionType >::running, and ThreadPool< FunctionType >::task_queue.
Referenced by ThreadPool< FunctionType >::wait().
{
while(running.size() < max_n && task_queue.size() > 0 ) {
FunctionType f(task_queue.front());
task_queue.pop_front();
boost::thread * p = new boost::thread(f);
thread_shptr t = thread_shptr(p);
running.push_back(t);
}
}

| void ThreadPool< FunctionType >::wait | ( | ) | [inline] |
Definition at line 47 of file ThreadPool.h.
References ThreadPool< FunctionType >::running, ThreadPool< FunctionType >::spawn(), and ThreadPool< FunctionType >::task_queue.
Referenced by ThreadPool< FunctionType >::~ThreadPool().
{
while(task_queue.size() > 0 || running.size() > 0) {
spawn();
for(std::list<thread_shptr>::iterator iter = running.begin();
iter != running.end(); ++iter) {
//std::cout << "timed wait\n";
if((*iter)->timed_join(boost::posix_time::millisec( 1000 ) )) {
std::list<thread_shptr>::iterator i(iter);
++iter;
running.erase(i);
}
}
}
}


unsigned int ThreadPool< FunctionType >::max_n [private] |
Definition at line 17 of file ThreadPool.h.
Referenced by ThreadPool< FunctionType >::spawn().
std::list<thread_shptr> ThreadPool< FunctionType >::running [private] |
Definition at line 20 of file ThreadPool.h.
Referenced by ThreadPool< FunctionType >::spawn(), and ThreadPool< FunctionType >::wait().
std::list<FunctionType> ThreadPool< FunctionType >::task_queue [private] |
Definition at line 19 of file ThreadPool.h.
Referenced by ThreadPool< FunctionType >::add(), ThreadPool< FunctionType >::spawn(), and ThreadPool< FunctionType >::wait().
1.7.4