1cd139dc7SShuo Chen// excerpts from http://code.google.com/p/muduo/
2cd139dc7SShuo Chen//
3cd139dc7SShuo Chen// Use of this source code is governed by a BSD-style license
4cd139dc7SShuo Chen// that can be found in the License file.
5cd139dc7SShuo Chen//
6cd139dc7SShuo Chen// Author: Shuo Chen (giantchen at gmail dot com)
7cd139dc7SShuo Chen
8cd139dc7SShuo Chen#ifndef MUDUO_BASE_THREADPOOL_H
9cd139dc7SShuo Chen#define MUDUO_BASE_THREADPOOL_H
10cd139dc7SShuo Chen
11cd139dc7SShuo Chen#include "Condition.h"
12cd139dc7SShuo Chen#include "Mutex.h"
13cd139dc7SShuo Chen#include "Thread.h"
14cd139dc7SShuo Chen
15cd139dc7SShuo Chen#include <boost/function.hpp>
16cd139dc7SShuo Chen#include <boost/noncopyable.hpp>
17cd139dc7SShuo Chen#include <boost/ptr_container/ptr_vector.hpp>
18cd139dc7SShuo Chen
19cd139dc7SShuo Chen#include <deque>
20cd139dc7SShuo Chen
21cd139dc7SShuo Chennamespace muduo
22cd139dc7SShuo Chen{
23cd139dc7SShuo Chen
24cd139dc7SShuo Chenclass ThreadPool : boost::noncopyable
25cd139dc7SShuo Chen{
26cd139dc7SShuo Chen public:
27cd139dc7SShuo Chen  typedef boost::function<void ()> Task;
28cd139dc7SShuo Chen
29cd139dc7SShuo Chen  explicit ThreadPool(const std::string& name = std::string());
30cd139dc7SShuo Chen  ~ThreadPool();
31cd139dc7SShuo Chen
32cd139dc7SShuo Chen  void start(int numThreads);
33cd139dc7SShuo Chen  void stop();
34cd139dc7SShuo Chen
35cd139dc7SShuo Chen  void run(const Task& f);
36cd139dc7SShuo Chen
37cd139dc7SShuo Chen private:
38cd139dc7SShuo Chen  void runInThread();
39cd139dc7SShuo Chen  Task take();
40cd139dc7SShuo Chen
41cd139dc7SShuo Chen  MutexLock mutex_;
42cd139dc7SShuo Chen  Condition cond_;
43cd139dc7SShuo Chen  std::string name_;
44cd139dc7SShuo Chen  boost::ptr_vector<muduo::Thread> threads_;
45cd139dc7SShuo Chen  std::deque<Task> queue_;
46cd139dc7SShuo Chen  bool running_;
47cd139dc7SShuo Chen};
48cd139dc7SShuo Chen
49cd139dc7SShuo Chen}
50cd139dc7SShuo Chen
51cd139dc7SShuo Chen#endif
52