degate  0.1.2
Public Member Functions | Private Types | Private Member Functions | Private Attributes
ThreadPool< FunctionType > Class Template Reference

#include <ThreadPool.h>

List of all members.

Public Member Functions

 ThreadPool (unsigned int n=4)
 ~ThreadPool ()
void add (FunctionType f)
void wait ()

Private Types

typedef std::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_shptrrunning

Detailed Description

template<typename FunctionType>
class ThreadPool< FunctionType >

Definition at line 12 of file ThreadPool.h.


Member Typedef Documentation

template<typename FunctionType >
typedef std::shared_ptr<boost::thread> ThreadPool< FunctionType >::thread_shptr [private]

Definition at line 14 of file ThreadPool.h.


Constructor & Destructor Documentation

template<typename FunctionType >
ThreadPool< FunctionType >::ThreadPool ( unsigned int  n = 4) [inline]

Definition at line 35 of file ThreadPool.h.

                                 : max_n(n) {
  }
template<typename FunctionType >
ThreadPool< FunctionType >::~ThreadPool ( ) [inline]

Definition at line 38 of file ThreadPool.h.

References ThreadPool< FunctionType >::wait().

                {
    wait();
  }

Here is the call graph for this function:


Member Function Documentation

template<typename FunctionType >
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);
  }
template<typename FunctionType >
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);
      }
  }

Here is the caller graph for this function:

template<typename FunctionType >
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);
        }

      }


    }

  }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

template<typename FunctionType >
unsigned int ThreadPool< FunctionType >::max_n [private]

Definition at line 17 of file ThreadPool.h.

Referenced by ThreadPool< FunctionType >::spawn().

template<typename FunctionType >
std::list<thread_shptr> ThreadPool< FunctionType >::running [private]
template<typename FunctionType >
std::list<FunctionType> ThreadPool< FunctionType >::task_queue [private]

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