00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #pragma once
00019
00020 #include <boost/function.hpp>
00021 #include <boost/bind.hpp>
00022 #undef assert
00023 #define assert MONGO_assert
00024
00025 namespace mongo {
00026
00027 namespace threadpool {
00028 class Worker;
00029
00030 typedef boost::function<void(void)> Task;
00031
00032
00033 class ThreadPool : boost::noncopyable {
00034 public:
00035 explicit ThreadPool(int nThreads=8);
00036
00037
00038
00039 ~ThreadPool();
00040
00041
00042
00043
00044 void join();
00045
00046
00047 void schedule(Task task);
00048
00049
00050
00051 template<typename F, typename A>
00052 void schedule(F f, A a) { schedule(boost::bind(f,a)); }
00053 template<typename F, typename A, typename B>
00054 void schedule(F f, A a, B b) { schedule(boost::bind(f,a,b)); }
00055 template<typename F, typename A, typename B, typename C>
00056 void schedule(F f, A a, B b, C c) { schedule(boost::bind(f,a,b,c)); }
00057 template<typename F, typename A, typename B, typename C, typename D>
00058 void schedule(F f, A a, B b, C c, D d) { schedule(boost::bind(f,a,b,c,d)); }
00059 template<typename F, typename A, typename B, typename C, typename D, typename E>
00060 void schedule(F f, A a, B b, C c, D d, E e) { schedule(boost::bind(f,a,b,c,d,e)); }
00061
00062 int tasks_remaining() { return _tasksRemaining; }
00063
00064 private:
00065 mongo::mutex _mutex;
00066 boost::condition _condition;
00067
00068 list<Worker*> _freeWorkers;
00069 list<Task> _tasks;
00070 int _tasksRemaining;
00071 int _nThreads;
00072
00073
00074 void task_done(Worker* worker);
00075 friend class Worker;
00076 };
00077
00078 }
00079
00080 using threadpool::ThreadPool;
00081
00082 }