Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnThreadPool.h
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnThreadPool.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 ** (based on code by David Breeden <breeden@cs.stanford.edu>
11 ** and Ian Goodfellow <ia3n@cs.stanford.edu>)
12 **
13 *****************************************************************************/
14 
15 #pragma once
16 #include <queue>
17 
18 #if defined(_WIN32)||defined(WIN32)||defined(__WIN32__)
19 #define DRWN_USE_WIN32THREADS
20 #else
21 #define DRWN_USE_PTHREADS
22 #endif
23 
24 #ifdef DRWN_USE_PTHREADS
25 #include <pthread.h>
26 #endif
27 
28 #ifdef DRWN_USE_WIN32THREADS
29 #include <windows.h>
30 #include <process.h>
31 #undef min
32 #undef max
33 #endif
34 
35 using namespace std;
36 
37 // drwnThreadJob --------------------------------------------------------------
38 
39 class drwnThreadPool;
40 
44 
46  friend class drwnThreadPool;
47 
48  private:
49  drwnThreadPool *_owner;
50  unsigned _threadId;
51  bool _bAquiredLock;
52 
53  public:
54  drwnThreadJob();
55  virtual ~drwnThreadJob();
56 
58  virtual void operator()() = 0;
59 
60  protected:
64  inline unsigned threadId() { return _threadId; }
65 
66  void lock();
67  void unlock();
68 };
69 
70 // drwnThreadPool -------------------------------------------------------------
71 
75 
77  friend class drwnThreadJob;
78 
79  public:
80  static unsigned MAX_THREADS;
81 
82  public:
84  drwnThreadPool(const unsigned size = MAX_THREADS);
85  ~drwnThreadPool();
86 
88  void start();
89 
91  void addJob(drwnThreadJob* job);
92 
94  void finish(bool bShowStatus = false);
95 
97  unsigned numThreads() const { return _nThreads; }
99  unsigned numJobsRemaining();
100 
101 private:
102  // main thread function
103 #ifdef DRWN_USE_PTHREADS
104  static void *runJobs(void *argPtr);
105 #endif
106 #ifdef DRWN_USE_WIN32THREADS
107  static unsigned __stdcall runJobs(void *argPtr);
108 #endif
109 
110 #if defined(DRWN_USE_PTHREADS)||defined(DRWN_USE_WIN32THREADS)
111  struct drwnThreadArgs {
112  drwnThreadPool *owner;
113  unsigned threadId;
114  int jobsProcessed;
115  };
116 
117  queue<drwnThreadJob *> _jobQ;
118 
119  unsigned _nThreads;
120 
121 #ifdef DRWN_USE_PTHREADS
122  pthread_t *_threads;
123  drwnThreadArgs *_args;
124 
125  pthread_mutex_t _mutex;
126  pthread_cond_t _cond;
127 #endif
128 #ifdef DRWN_USE_WIN32THREADS
129  HANDLE *_threads;
130  drwnThreadArgs *_args;
131 
132  CRITICAL_SECTION _mutex;
133  CONDITION_VARIABLE _cond;
134 #endif
135 
136  bool _bQuit;
137 
138  bool _bProfilerEnabled;
139 #else
140  unsigned _nThreads;
141  void *_threads;
142  void *_args;
143 #endif
144 };
145 
Implements a pool of threads for running concurrent jobs.
Definition: drwnThreadPool.h:76
Interface for a thread job functor.
Definition: drwnThreadPool.h:45
static unsigned MAX_THREADS
maximum number of threads allowed
Definition: drwnThreadPool.h:80
unsigned threadId()
obtain the id for the thread running (or that ran) this job. The return value is guaranteed to be bet...
Definition: drwnThreadPool.h:64
unsigned numThreads() const
return the number of threads running
Definition: drwnThreadPool.h:97