Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ThreadPool.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * ThreadPool.h
17  * An executor which farms work out to a bunch of threads.
18  * Copyright (C) 2011 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLA_THREAD_THREADPOOL_H_
22 #define INCLUDE_OLA_THREAD_THREADPOOL_H_
23 
24 #include <ola/Callback.h>
25 #include <ola/thread/ConsumerThread.h>
26 #include <ola/thread/Thread.h>
27 #include <queue>
28 #include <vector>
29 
30 namespace ola {
31 namespace thread {
32 
33 class ThreadPool {
34  public :
36 
37  explicit ThreadPool(unsigned int thread_count)
38  : m_thread_count(thread_count),
39  m_shutdown(false) {
40  }
41  ~ThreadPool();
42  bool Init();
43  void JoinAll();
44  void Execute(Action action);
45 
46  private:
47  queue<Action> m_callback_queue;
48  unsigned int m_thread_count;
49  bool m_shutdown;
50  Mutex m_mutex;
51  ConditionVariable m_condition_var;
52  std::vector<ConsumerThread*> m_threads;
53 
54  void JoinAllThreads();
55 };
56 } // namespace thread
57 } // namespace ola
58 #endif // INCLUDE_OLA_THREAD_THREADPOOL_H_