MongoDB  2.7.0
thread_pool.h
1 // thread_pool.h
2 
3 /* Copyright 2009 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <list>
21 
22 #include <boost/bind.hpp>
23 #include <boost/function.hpp>
24 #include <boost/thread/condition.hpp>
25 
26 #include "mongo/util/concurrency/mutex.h"
27 
28 namespace mongo {
29 
30  namespace threadpool {
31  class Worker;
32 
33  typedef boost::function<void(void)> Task; //nullary function or functor
34 
35  // exported to the mongo namespace
36  class ThreadPool : boost::noncopyable {
37  public:
38  explicit ThreadPool(int nThreads=8);
39 
40  // blocks until all tasks are complete (tasks_remaining() == 0)
41  // You should not call schedule while in the destructor
42  ~ThreadPool();
43 
44  // blocks until all tasks are complete (tasks_remaining() == 0)
45  // does not prevent new tasks from being scheduled so could wait forever.
46  // Also, new tasks could be scheduled after this returns.
47  void join();
48 
49  // task will be copied a few times so make sure it's relatively cheap
50  void schedule(Task task);
51 
52  // Helpers that wrap schedule and boost::bind.
53  // Functor and args will be copied a few times so make sure it's relatively cheap
54  template<typename F, typename A>
55  void schedule(F f, A a) { schedule(boost::bind(f,a)); }
56  template<typename F, typename A, typename B>
57  void schedule(F f, A a, B b) { schedule(boost::bind(f,a,b)); }
58  template<typename F, typename A, typename B, typename C>
59  void schedule(F f, A a, B b, C c) { schedule(boost::bind(f,a,b,c)); }
60  template<typename F, typename A, typename B, typename C, typename D>
61  void schedule(F f, A a, B b, C c, D d) { schedule(boost::bind(f,a,b,c,d)); }
62  template<typename F, typename A, typename B, typename C, typename D, typename E>
63  void schedule(F f, A a, B b, C c, D d, E e) { schedule(boost::bind(f,a,b,c,d,e)); }
64 
65  int tasks_remaining() { return _tasksRemaining; }
66 
67  private:
68  mongo::mutex _mutex;
69  boost::condition _condition;
70 
71  std::list<Worker*> _freeWorkers; //used as LIFO stack (always front)
72  std::list<Task> _tasks; //used as FIFO queue (push_back, pop_front)
73  int _tasksRemaining; // in queue + currently processing
74  int _nThreads; // only used for sanity checking. could be removed in the future.
75 
76  // should only be called by a worker from the worker's thread
77  void task_done(Worker* worker);
78  friend class Worker;
79  };
80 
81  } //namespace threadpool
82 
84 
85 } //namespace mongo
Definition: thread_pool.h:36
On pthread systems, it is an error to destroy a mutex while held (boost mutex may use pthread)...
Definition: mutex.h:74