opensurgsim
ThreadPool.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_FRAMEWORK_THREADPOOL_H
17 #define SURGSIM_FRAMEWORK_THREADPOOL_H
18 
19 #include <atomic>
20 #include <boost/thread.hpp>
21 #include <functional>
22 #include <future>
23 #include <list>
24 #include <memory>
25 #include <queue>
26 
27 
28 namespace SurgSim
29 {
30 namespace Framework
31 {
32 
66 {
67 public:
70  explicit ThreadPool(size_t numThreads = boost::thread::hardware_concurrency());
71 
73  ~ThreadPool();
74 
81  template <class R>
82  std::future<R> enqueue(std::function<R()> function);
83 
84 private:
87  ThreadPool(const ThreadPool& other);
88  ThreadPool& operator=(const ThreadPool& other);
90 
92  class TaskBase;
93 
95  template<class R>
96  class Task;
97 
99  std::list<boost::thread> m_threads;
100 
102  std::queue<std::unique_ptr<TaskBase>> m_tasks;
103 
105  boost::mutex m_mutex;
106 
108  boost::condition_variable m_threadSignaler;
109 
111  std::atomic<bool> m_destructing;
112 };
113 
114 };
115 };
116 
117 #include "SurgSim/Framework/ThreadPool-inl.h"
118 
119 #endif //SURGSIM_FRAMEWORK_THREADPOOL_H
120 
121 
122 
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
Actual tasks, with typed return type.
Definition: ThreadPool-inl.h:34
Definition: ThreadPool-inl.h:25
ThreadPool(size_t numThreads=boost::thread::hardware_concurrency())
Constructor.
Definition: ThreadPool.cpp:25
std::future< R > enqueue(std::function< R()> function)
Queue a task to be run by the ThreadPool.
Definition: ThreadPool-inl.h:57
A thread pool for completing heterogenous tasks.
Definition: ThreadPool.h:65
~ThreadPool()
Desctructor.
Definition: ThreadPool.cpp:60