CUGL
Cornell University Game Library
CUThreadPool.h
1 //
2 // CUThreadPool.h
3 // Cornell University Game Library (CUGL)
4 //
5 // Module for a pool of threads capable of executing asynchronous tasks. Each
6 // task is specified by a void function. There are no guarantees about thread
7 // safety; that is responsibility of the author of each task.
8 //
9 // This code is largely inspired from the Cocos2d file AudioEngine.cpp, from
10 // the code for asynchronous asset loading. We generalized that class added
11 // some notable safety changes.
12 //
13 // CUGL zlib License:
14 // This software is provided 'as-is', without any express or implied
15 // warranty. In no event will the authors be held liable for any damages
16 // arising from the use of this software.
17 //
18 // Permission is granted to anyone to use this software for any purpose,
19 // including commercial applications, and to alter it and redistribute it
20 // freely, subject to the following restrictions:
21 //
22 // 1. The origin of this software must not be misrepresented; you must not
23 // claim that you wrote the original software. If you use this software
24 // in a product, an acknowledgment in the product documentation would be
25 // appreciated but is not required.
26 //
27 // 2. Altered source versions must be plainly marked as such, and must not
28 // be misrepresented as being the original software.
29 //
30 // 3. This notice may not be removed or altered from any source distribution.
31 //
32 // Author: Walker White
33 // Version: 11/29/16
34 //
35 #ifndef __CU_THREAD_POOL_H__
36 #define __CU_THREAD_POOL_H__
37 #include <cugl/base/CUBase.h>
38 #include <SDL/SDL.h>
39 #include <condition_variable>
40 #include <stdio.h>
41 #include <queue>
42 #include <vector>
43 #include <thread>
44 
45 // std::thread is not safe on Android and Windows platforms
46 #if defined (__WINDOWS__) || defined (__ANDROID__)
47  #define CU_SDL_THREADS 1
48 #endif
49 
50 namespace cugl {
51 
52 #pragma mark -
53 #pragma mark Thread Pool
54 
75 class ThreadPool {
76 private:
78 #ifdef CU_SDL_THREADS
79  std::vector<SDL_Thread*> _workers;
80 #else
81  std::vector<std::thread> _workers;
82 #endif
83 
85  std::queue< std::function<void()> > _taskQueue;
86 
88  std::mutex _queueMutex;
90  std::condition_variable _taskCondition;
91 
93  bool _stop;
95  int _complete;
96 
104  void threadFunc();
105 
114  static int sdlThreadFunc(void* ptr);
115 
116 
117 #pragma mark Constructors
118 public:
127  ThreadPool() :_stop(false), _complete(0) { }
128 
138 
147  void dispose();
148 
160  virtual bool init(int threads = 4);
161 
162 
163 #pragma mark Static Constructors
164 
175  static std::shared_ptr<ThreadPool> alloc(int threads = 4) {
176  std::shared_ptr<ThreadPool> result = std::make_shared<ThreadPool>();
177  return (result->init(threads) ? result : nullptr);
178  }
179 
180 
181 #pragma mark Task Management
182 
192  void addTask(const std::function<void()> &task);
193 
201  void stop();
202 
212  bool isStopped() const { return _stop; }
213 
221  bool isShutdown() const { return _workers.size() == _complete; }
222 
223  // Copying is only allowed via shared pointer.
224  CU_DISALLOW_COPY_AND_ASSIGN(ThreadPool);
225 };
226 
227 }
228 
229 #endif /* __CU_THREAD_POOL_H__ */
bool isStopped() const
Definition: CUThreadPool.h:212
bool isShutdown() const
Definition: CUThreadPool.h:221
static std::shared_ptr< ThreadPool > alloc(int threads=4)
Definition: CUThreadPool.h:175
void addTask(const std::function< void()> &task)
virtual bool init(int threads=4)
Definition: CUThreadPool.h:75
ThreadPool()
Definition: CUThreadPool.h:127
~ThreadPool()
Definition: CUThreadPool.h:137
Definition: CUAnimationNode.h:52