All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
ana::ThreadPool Class Reference

A very simple thread pool for use by Surface. More...

#include <ThreadPool.h>

Classes

struct  Task
 

Public Member Functions

 ThreadPool (unsigned int maxThreads=0)
 
virtual ~ThreadPool ()
 
void ShowProgress (const std::string &title)
 
void Finish ()
 Wait for all threads to complete before returning. More...
 
template<class F , class... A>
void AddTask (F func, A...args)
 Add task with arguments. More...
 
template<class T , class M , class... A>
void AddMemberTask (T *obj, M meth, A...args)
 Add member function task, with arguments. More...
 

Protected Types

typedef std::function< void(void)> func_t
 The type of the user's worker functions. More...
 

Protected Member Functions

void AddTaskHelper (func_t func)
 

Static Protected Member Functions

static void * WorkerFunc (void *arg)
 

Protected Attributes

unsigned int fMaxThreads
 
pthread_mutex_t fTasksLock
 
std::deque< func_tfTasks
 Actually, this is protecting fNumLiveThreads. More...
 
pthread_mutex_t fThreadsLock
 
std::vector< pthread_t > fThreads
 All threads we ever created. More...
 
unsigned int fNumLiveThreads
 Number of threads that are running. More...
 
pthread_mutex_t fProgressLock
 Protects fTasksCompleted and fTotalTasks too. More...
 
int fTasksCompleted
 
int fTotalTasks
 How many tasks have we ever seen? More...
 
ProgressfProgress
 

Detailed Description

A very simple thread pool for use by Surface.

With great power comes great responsibility. Use caution on the grid or on shared interactive machines before you get yelled at or banned.

Definition at line 17 of file ThreadPool.h.

Member Typedef Documentation

typedef std::function<void(void)> ana::ThreadPool::func_t
protected

The type of the user's worker functions.

Use std::bind etc to pass arguments

Definition at line 43 of file ThreadPool.h.

Constructor & Destructor Documentation

ana::ThreadPool::ThreadPool ( unsigned int  maxThreads = 0)
explicit
Parameters
maxThreadsMaximum number of threads to use at one time. If unspecified, uses number of cores in machine. Use great caution on the grid or on shared interactive machines.

Definition at line 11 of file ThreadPool.cxx.

12  : fMaxThreads(maxThreads), fNumLiveThreads(0),
14  {
15  if(maxThreads == 0){
16  fMaxThreads = sysconf(_SC_NPROCESSORS_ONLN);
17  }
18 
19  pthread_mutex_init(&fTasksLock, 0);
20  pthread_mutex_init(&fThreadsLock, 0);
21  pthread_mutex_init(&fProgressLock, 0);
22  }
pthread_mutex_t fThreadsLock
Definition: ThreadPool.h:58
Progress * fProgress
Definition: ThreadPool.h:66
int fTotalTasks
How many tasks have we ever seen?
Definition: ThreadPool.h:65
pthread_mutex_t fProgressLock
Protects fTasksCompleted and fTotalTasks too.
Definition: ThreadPool.h:63
unsigned int fMaxThreads
Definition: ThreadPool.h:50
pthread_mutex_t fTasksLock
Definition: ThreadPool.h:54
unsigned int fNumLiveThreads
Number of threads that are running.
Definition: ThreadPool.h:60
ana::ThreadPool::~ThreadPool ( )
virtual

Definition at line 25 of file ThreadPool.cxx.

26  {
27  // This is safe, even if it's already been explicitly called
28  Finish();
29 
30  pthread_mutex_destroy(&fTasksLock);
31  pthread_mutex_destroy(&fThreadsLock);
32  pthread_mutex_destroy(&fProgressLock);
33 
34  delete fProgress;
35  }
pthread_mutex_t fThreadsLock
Definition: ThreadPool.h:58
void Finish()
Wait for all threads to complete before returning.
Definition: ThreadPool.cxx:44
Progress * fProgress
Definition: ThreadPool.h:66
pthread_mutex_t fProgressLock
Protects fTasksCompleted and fTotalTasks too.
Definition: ThreadPool.h:63
pthread_mutex_t fTasksLock
Definition: ThreadPool.h:54

Member Function Documentation

template<class T , class M , class... A>
void ana::ThreadPool::AddMemberTask ( T *  obj,
meth,
A...  args 
)
inline

Add member function task, with arguments.

Definition at line 78 of file ThreadPool.h.

79 {
80  AddTaskHelper(std::bind(meth, obj, args...));
81 }
void AddTaskHelper(func_t func)
Definition: ThreadPool.cxx:92
template<class F , class... A>
void ana::ThreadPool::AddTask ( F  func,
A...  args 
)
inline

Add task with arguments.

Definition at line 72 of file ThreadPool.h.

73 {
74  AddTaskHelper(std::bind(func, args...));
75 }
void AddTaskHelper(func_t func)
Definition: ThreadPool.cxx:92
void ana::ThreadPool::AddTaskHelper ( func_t  func)
protected

Definition at line 92 of file ThreadPool.cxx.

93  {
94  pthread_mutex_lock(&fTasksLock);
95  fTasks.push_back(func);
96  pthread_mutex_unlock(&fTasksLock);
97 
98  pthread_mutex_lock(&fProgressLock);
99  ++fTotalTasks;
100  pthread_mutex_unlock(&fProgressLock);
101 
102  pthread_mutex_lock(&fThreadsLock);
104  fThreads.push_back(pthread_t());
105  pthread_create(&fThreads.back(), 0, WorkerFunc, this);
106  ++fNumLiveThreads;
107  }
108  pthread_mutex_unlock(&fThreadsLock);
109  }
pthread_mutex_t fThreadsLock
Definition: ThreadPool.h:58
std::vector< pthread_t > fThreads
All threads we ever created.
Definition: ThreadPool.h:59
int fTotalTasks
How many tasks have we ever seen?
Definition: ThreadPool.h:65
pthread_mutex_t fProgressLock
Protects fTasksCompleted and fTotalTasks too.
Definition: ThreadPool.h:63
std::deque< func_t > fTasks
Actually, this is protecting fNumLiveThreads.
Definition: ThreadPool.h:55
static void * WorkerFunc(void *arg)
Definition: ThreadPool.cxx:57
unsigned int fMaxThreads
Definition: ThreadPool.h:50
pthread_mutex_t fTasksLock
Definition: ThreadPool.h:54
unsigned int fNumLiveThreads
Number of threads that are running.
Definition: ThreadPool.h:60
void ana::ThreadPool::Finish ( )

Wait for all threads to complete before returning.

Definition at line 44 of file ThreadPool.cxx.

45  {
46  void* junk;
47  for(pthread_t& th: fThreads) pthread_join(th, &junk);
48 
49  assert(fNumLiveThreads == 0);
50 
51  fThreads.clear(); // Make it safe to call a second time
52 
53  if(fProgress) fProgress->Done();
54  }
std::vector< pthread_t > fThreads
All threads we ever created.
Definition: ThreadPool.h:59
Progress * fProgress
Definition: ThreadPool.h:66
void Done()
Call this when action is completed.
Definition: Progress.cxx:91
unsigned int fNumLiveThreads
Number of threads that are running.
Definition: ThreadPool.h:60
void ana::ThreadPool::ShowProgress ( const std::string &  title)

Definition at line 38 of file ThreadPool.cxx.

39  {
40  if(!fProgress) fProgress = new Progress(title);
41  }
Progress * fProgress
Definition: ThreadPool.h:66
void * ana::ThreadPool::WorkerFunc ( void *  arg)
staticprotected

Definition at line 57 of file ThreadPool.cxx.

58  {
59  // We smuggle essentially the this pointer in through the argument
60  ThreadPool* pool = (ThreadPool*)arg;
61 
62  while(true){
63  pthread_mutex_lock(&pool->fTasksLock);
64  if(pool->fTasks.empty()){
65  // Nothing to do, commit suicide
66  pthread_mutex_unlock(&pool->fTasksLock);
67 
68  pthread_mutex_lock(&pool->fThreadsLock);
69  --pool->fNumLiveThreads;
70  pthread_mutex_unlock(&pool->fThreadsLock);
71 
72  return 0;
73  }
74  // Get a task to do
75  func_t task = pool->fTasks.front();
76  pool->fTasks.pop_front();
77  pthread_mutex_unlock(&pool->fTasksLock);
78 
79  // Actually do the user's work
80  task();
81 
82  pthread_mutex_lock(&pool->fProgressLock);
83  ++pool->fTasksCompleted;
84 
85  if(pool->fProgress)
86  pool->fProgress->SetProgress(pool->fTasksCompleted/double(pool->fTotalTasks));
87  pthread_mutex_unlock(&pool->fProgressLock);
88  }
89  }
std::function< void(void)> func_t
The type of the user&#39;s worker functions.
Definition: ThreadPool.h:43
ThreadPool(unsigned int maxThreads=0)
Definition: ThreadPool.cxx:11

Member Data Documentation

unsigned int ana::ThreadPool::fMaxThreads
protected

Definition at line 50 of file ThreadPool.h.

unsigned int ana::ThreadPool::fNumLiveThreads
protected

Number of threads that are running.

Definition at line 60 of file ThreadPool.h.

Progress* ana::ThreadPool::fProgress
protected

Definition at line 66 of file ThreadPool.h.

pthread_mutex_t ana::ThreadPool::fProgressLock
protected

Protects fTasksCompleted and fTotalTasks too.

Definition at line 63 of file ThreadPool.h.

std::deque<func_t> ana::ThreadPool::fTasks
protected

Actually, this is protecting fNumLiveThreads.

Definition at line 55 of file ThreadPool.h.

int ana::ThreadPool::fTasksCompleted
protected

Definition at line 64 of file ThreadPool.h.

pthread_mutex_t ana::ThreadPool::fTasksLock
protected

Definition at line 54 of file ThreadPool.h.

std::vector<pthread_t> ana::ThreadPool::fThreads
protected

All threads we ever created.

Definition at line 59 of file ThreadPool.h.

pthread_mutex_t ana::ThreadPool::fThreadsLock
protected

Definition at line 58 of file ThreadPool.h.

int ana::ThreadPool::fTotalTasks
protected

How many tasks have we ever seen?

Definition at line 65 of file ThreadPool.h.


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