ThreadPool_test.cc revision cd139dc7
1cd139dc7SShuo Chen#include "../ThreadPool.h" 2cd139dc7SShuo Chen#include "../CountDownLatch.h" 3cd139dc7SShuo Chen 4cd139dc7SShuo Chen#include <boost/bind.hpp> 5cd139dc7SShuo Chen#include <stdio.h> 6cd139dc7SShuo Chen 7cd139dc7SShuo Chenvoid print() 8cd139dc7SShuo Chen{ 9cd139dc7SShuo Chen printf("tid=%d\n", muduo::CurrentThread::tid()); 10cd139dc7SShuo Chen} 11cd139dc7SShuo Chen 12cd139dc7SShuo Chenvoid printString(const std::string& str) 13cd139dc7SShuo Chen{ 14cd139dc7SShuo Chen printf("tid=%d, str=%s\n", muduo::CurrentThread::tid(), str.c_str()); 15cd139dc7SShuo Chen} 16cd139dc7SShuo Chen 17cd139dc7SShuo Chenint main() 18cd139dc7SShuo Chen{ 19cd139dc7SShuo Chen muduo::ThreadPool pool("MainThreadPool"); 20cd139dc7SShuo Chen pool.start(5); 21cd139dc7SShuo Chen 22cd139dc7SShuo Chen pool.run(print); 23cd139dc7SShuo Chen pool.run(print); 24cd139dc7SShuo Chen for (int i = 0; i < 100; ++i) 25cd139dc7SShuo Chen { 26cd139dc7SShuo Chen char buf[32]; 27cd139dc7SShuo Chen snprintf(buf, sizeof buf, "task %d", i); 28cd139dc7SShuo Chen pool.run(boost::bind(printString, std::string(buf))); 29cd139dc7SShuo Chen } 30cd139dc7SShuo Chen 31cd139dc7SShuo Chen muduo::CountDownLatch latch(1); 32cd139dc7SShuo Chen pool.run(boost::bind(&muduo::CountDownLatch::countDown, &latch)); 33cd139dc7SShuo Chen latch.wait(); 34cd139dc7SShuo Chen pool.stop(); 35cd139dc7SShuo Chen} 36cd139dc7SShuo Chen 37