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