iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BottomlessThreadPool.H
Go to the documentation of this file.
1 /*! @file
2  @author Randolph Voorhies
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 #ifndef INCLUDE_NRT_CORE_UTIL_BOTTOMLESSTHREADPOOL_H
36 #define INCLUDE_NRT_CORE_UTIL_BOTTOMLESSTHREADPOOL_H
37 
38 #include <future>
39 #include <thread>
40 #include <vector>
41 #include <queue>
42 #include <condition_variable>
43 
45 
46 namespace nrt
47 {
48  //! A thread pool with an unlimited number of threads
49  /*! Unlike the vanilla ThreadPool implementation, BottomlessThreadPool will
50  always execute your job immediately. It does so by either passing it off
51  to an idle thread (if one is available in the pool), or by creating a new
52  thread (if all threads in the pool are currently busy). */
54  {
55 
56  public:
57  //! Constructor
58  BottomlessThreadPool(size_t const initialThreads = std::thread::hardware_concurrency());
59 
60  //! Destructor
62 
63  template <class Func>
64  std::future<typename std::result_of<Func()>::type>
65  pushJob( Func f );
66 
67  //! Execute a new job (with an arbitrary return type) in the thread pool
68  //template<class ReturnType>
69  // std::future<ReturnType> pushJob(std::function<ReturnType()> job);
70 
71  //! Get the number of threads in the pool
72  size_t numThreads();
73 
74  private:
75 
76  // A composite of a condition variable with function to act as a channel
77  // between workers and the pool
78  struct Channel
79  {
80  std::condition_variable condition;
81  FunctionWrapper job;
82  //std::function<void()> job;
83  };
84 
85  // A thread pool worker
86  class Worker
87  {
88  public:
89  Worker(BottomlessThreadPool * sourcePool, std::shared_ptr<Channel> channel, size_t id, FunctionWrapper && job);
90  //Worker(BottomlessThreadPool * sourcePool, std::shared_ptr<Channel> channel, size_t id, std::function<void()> job);
91  ~Worker();
92 
93  private:
94  void workThread(FunctionWrapper && initialJob);
95  //void workThread(std::function<void()> initialJob);
96  std::shared_ptr<Channel> itsChannel;
97  size_t itsId;
98  BottomlessThreadPool * itsSourcePool;
99  bool itsRunning;
100  std::thread itsThread;
101  };
102 
103  // Push a wrapped up job (as a void() function) onto an idle worker if
104  // any are available, otherwise start a new worker
105  //void pushJobWrapper(std::function<void()> jobWrapper);
106 
107  std::vector<std::unique_ptr<Worker>> itsWorkers;
108 
109  std::mutex itsMtx;
110  std::vector<std::shared_ptr<Channel>> itsChannels;
111  std::queue<size_t> itsOpenChannels;
112  };
113 
114  //! Execute a new job (with an void return type) in the thread pool
115  //template<>
116  //std::future<void> BottomlessThreadPool::pushJob(std::function<void()> job);
117 
118 }
119 
121 
122 #endif // INCLUDE_NRT_CORE_UTIL_BOTTOMLESSTHREADPOOL_H