Allolib  1.0
C++ Components For Interactive Multimedia
al_Thread.hpp
1 #ifndef INCLUDE_AL_THREAD_HPP
2 #define INCLUDE_AL_THREAD_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology,
8  UCSB. Copyright (C) 2012. The Regents of the University of California. All
9  rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are
13  met:
14 
15  Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17 
18  Redistributions in binary form must reproduce the above
19  copyright notice, this list of conditions and the following disclaimer in the
20  documentation and/or other materials provided with the
21  distribution.
22 
23  Neither the name of the University of California nor the names
24  of its contributors may be used to endorse or promote products derived from
25  this software without specific prior written permission.
26 
27  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
31  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
34  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 
39 
40  File description:
41  Minimal thread class with similar interface to C++0x thread
42 
43  File author(s):
44  Lance Putnam, 2010, putnam.lance@gmail.com
45  Graham Wakefield, 2010, grrrwaaa@gmail.com
46 */
47 
48 namespace al {
49 
52  virtual ~ThreadFunction() {}
53 
55  virtual void operator()() = 0;
56 };
57 
61  typedef void *(*CFunc)(void *userData);
62 
66  CThreadFunction(CFunc threadFunc = 0, void *userData = 0)
67  : func(threadFunc), user(userData) {}
68 
69  void operator()() { func(user); }
70 
72  void *user;
73 };
74 
78 class Thread {
79 public:
81  Thread();
82 
85 
88  Thread(void *(*cFunc)(void *userData), void *userData);
89 
91  Thread(const Thread &other);
92 
93  ~Thread();
94 
96  Thread &joinOnDestroy(bool v) {
97  mJoinOnDestroy = v;
98  return *this;
99  }
100 
102 
105  Thread &priority(int v);
106 
108  bool start(ThreadFunction &func);
109 
111  bool start(void *(*threadFunc)(void *userData), void *userData);
112 
114 
120  bool join();
121 
123 
126  static void *current();
127 
128  // Stuff for assignment
129  friend void swap(Thread &a, Thread &b);
130  Thread &operator=(Thread other);
131 
132 protected:
133  struct Impl;
134  Impl *mImpl;
135  CThreadFunction mCFunc;
136  bool mJoinOnDestroy;
137 };
138 
142 template <class ThreadFunction> class Threads {
143 public:
145  struct Worker {
146  Thread thread;
147  ThreadFunction function;
148  };
149 
151  Threads(int size = 0) : mSize(0), mWorkers(0) { resize(size); }
152 
153  ~Threads() { clear(); }
154 
156  int size() const { return mSize; }
157 
159  void resize(int n) {
160  if (n != size()) {
161  mSize = n;
162  clear();
163  mWorkers = new Worker[size()];
164  }
165  }
166 
168  void start(bool joinAll = true) {
169  for (int i = 0; i < size(); ++i) {
170  thread(i).start(function(i));
171  }
172  if (joinAll)
173  join();
174  }
175 
177  void join() {
178  for (int i = 0; i < size(); ++i)
179  thread(i).join();
180  }
181 
183  Worker &worker(int i) { return mWorkers[i]; }
184 
186  Thread &thread(int i) { return worker(i).thread; }
187 
189  ThreadFunction &function(int i) { return worker(i).function; }
190 
192 
196  template <class T> double range(T max, T min = T(0)) {
197  return (max - min) / double(size());
198  }
199 
201 
207  template <class T> void getInterval(T *interval, int i, T max, T min = T(0)) {
208  double diam = range(max, min);
209  double left = diam * i + min;
210  interval[0] = left;
211  interval[1] = left + diam;
212  }
213 
214 protected:
215  int mSize;
216  Worker *mWorkers;
217 
218  void clear() {
219  if (mWorkers)
220  delete[] mWorkers;
221  }
222 };
223 
224 // -----------------------------------------------------------------------------
225 // Inline implementation
226 
227 inline bool Thread::start(void *(*threadFunc)(void *userData), void *userData) {
228  mCFunc.func = threadFunc;
229  mCFunc.user = userData;
230  return start(mCFunc);
231 }
232 
233 } // namespace al
234 
235 #endif
Thread(const Thread &other)
Copy constructor.
Thread()
Create thread without starting.
Thread(void *(*cFunc)(void *userData), void *userData)
static void * current()
Return pointer to current OS thread object.
bool start(ThreadFunction &func)
Start executing thread function.
bool join()
Block the calling routine indefinitely until the thread terminates.
Thread(ThreadFunction &func)
Thread & priority(int v)
Set thread priority.
Thread & joinOnDestroy(bool v)
Set whether thread will automatically join upon destruction.
Definition: al_Thread.hpp:96
Thread & thread(int i)
Get a worker thread.
Definition: al_Thread.hpp:186
Worker & worker(int i)
Get a worker.
Definition: al_Thread.hpp:183
void join()
Join all worker threads.
Definition: al_Thread.hpp:177
void start(bool joinAll=true)
Start all worker threads.
Definition: al_Thread.hpp:168
int size() const
Returns number of workers.
Definition: al_Thread.hpp:156
double range(T max, T min=T(0))
Get worker sub-interval range of a full interval [min, max)
Definition: al_Thread.hpp:196
void resize(int n)
Resize number of workers.
Definition: al_Thread.hpp:159
void getInterval(T *interval, int i, T max, T min=T(0))
Get worker sub-interval of a full interval [min, max)
Definition: al_Thread.hpp:207
Threads(int size=0)
Definition: al_Thread.hpp:151
T min(const T &v1, const T &v2, const T &v3)
T max(const T &v1, const T &v2, const T &v3)
Definition: al_App.hpp:23
C-style thread function with user data.
Definition: al_Thread.hpp:59
void *(* CFunc)(void *userData)
Prototype of thread execution function.
Definition: al_Thread.hpp:61
CThreadFunction(CFunc threadFunc=0, void *userData=0)
Definition: al_Thread.hpp:66
void operator()()
Routine called on thread execution start.
Definition: al_Thread.hpp:69
void * user
User data passed into thread execution function.
Definition: al_Thread.hpp:72
CFunc func
Thread execution function.
Definition: al_Thread.hpp:71
Function object interface used by thread.
Definition: al_Thread.hpp:51
virtual void operator()()=0
Routine called on thread execution start.
A thread and function.
Definition: al_Thread.hpp:145