All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ThreadPool.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <deque>
4 #include <functional>
5 #include <vector>
6 
7 #include "pthread.h"
8 
9 namespace ana
10 {
11  class Progress;
12 
13  /// \brief A very simple thread pool for use by \ref Surface
14  ///
15  /// With great power comes great responsibility. Use caution on the grid or
16  /// on shared interactive machines before you get yelled at or banned.
17  class ThreadPool
18  {
19  public:
20  /// \param maxThreads Maximum number of threads to use at one time.
21  /// If unspecified, uses number of cores in machine.
22  /// Use great caution on the grid or on shared
23  /// interactive machines.
24  explicit ThreadPool(unsigned int maxThreads = 0);
25  virtual ~ThreadPool();
26 
27  void ShowProgress(const std::string& title);
28 
29  /// Wait for all threads to complete before returning
30  void Finish();
31 
32  /// Add task with arguments
33  template<class F, class... A> void AddTask(F func, A... args);
34 
35  /// Add member function task, with arguments
36  template<class T, class M, class... A> void AddMemberTask(T* obj, M meth, A... args);
37 
38 
39  protected:
40  /// \brief The type of the user's worker functions
41  ///
42  /// Use std::bind etc to pass arguments
43  typedef std::function<void(void)> func_t;
44 
45  void AddTaskHelper(func_t func);
46 
47 
48  static void* WorkerFunc(void* arg);
49 
50  unsigned int fMaxThreads;
51 
52  struct Task{func_t func; void* ctx;};
53 
54  pthread_mutex_t fTasksLock;
55  std::deque<func_t> fTasks;
56 
57  ///< Actually, this is protecting \ref fNumLiveThreads
58  pthread_mutex_t fThreadsLock;
59  std::vector<pthread_t> fThreads; ///< All threads we ever created
60  unsigned int fNumLiveThreads; ///< Number of threads that are running
61 
62  /// Protects \ref fTasksCompleted and \ref fTotalTasks too
63  pthread_mutex_t fProgressLock;
65  int fTotalTasks; ///< How many tasks have we ever seen?
67  };
68 }
69 
70 
71 /// Add task with arguments
72 template<class F, class... A> inline void ana::ThreadPool::AddTask(F func, A... args)
73 {
74  AddTaskHelper(std::bind(func, args...));
75 }
76 
77 /// Add member function task, with arguments
78 template<class T, class M, class... A> inline void ana::ThreadPool::AddMemberTask(T* obj, M meth, A... args)
79 {
80  AddTaskHelper(std::bind(meth, obj, args...));
81 }
pthread_mutex_t fThreadsLock
Definition: ThreadPool.h:58
std::vector< pthread_t > fThreads
All threads we ever created.
Definition: ThreadPool.h:59
void AddTask(F func, A...args)
Add task with arguments.
Definition: ThreadPool.h:72
void Finish()
Wait for all threads to complete before returning.
Definition: ThreadPool.cxx:44
Progress * fProgress
Definition: ThreadPool.h:66
std::function< void(void)> func_t
The type of the user&#39;s worker functions.
Definition: ThreadPool.h:43
void AddTaskHelper(func_t func)
Definition: ThreadPool.cxx:92
process_name opflashCryoW ana
ThreadPool(unsigned int maxThreads=0)
Definition: ThreadPool.cxx:11
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
virtual ~ThreadPool()
Definition: ThreadPool.cxx:25
std::deque< func_t > fTasks
Actually, this is protecting fNumLiveThreads.
Definition: ThreadPool.h:55
void AddMemberTask(T *obj, M meth, A...args)
Add member function task, with arguments.
Definition: ThreadPool.h:78
static void * WorkerFunc(void *arg)
Definition: ThreadPool.cxx:57
unsigned int fMaxThreads
Definition: ThreadPool.h:50
A very simple thread pool for use by Surface.
Definition: ThreadPool.h:17
A simple ascii-art progress bar.
Definition: Progress.h:9
void ShowProgress(const std::string &title)
Definition: ThreadPool.cxx:38
pthread_mutex_t fTasksLock
Definition: ThreadPool.h:54
float A
Definition: dedx.py:137
unsigned int fNumLiveThreads
Number of threads that are running.
Definition: ThreadPool.h:60