vlambda博客
学习文章列表

C++11线程池库及测试demo

C++11添加了线程实现,但是没有实现线程池,所以网上检索到一个比较靠谱的线程池实现,该库只有一个头文件,并且作者添加了中文注释。

下面demo是对线程池的封装及测试,对于学习C++11多线程以及使用线程池都很有帮助。


作者博客也对该线程池的实现做了详细的说明和讲解。

https://www.cnblogs.com/lzpong/p/6397997.html


https://github.com/lzpong/threadpool.git


https://gitee.com/yingge2017/cpp_demo.git


int main(int argc,char **argv){ cout<<"main----"<<endl; // originTest(); //test threadpoolengine threadpool* tpl = ThreadPoolEngine::instance()->generator(); cout<<hex<<tpl<<endl; cout<<dec<<"thread pool size:"<<tpl->thrCount()<<endl; for(int i = 0; i < 10; ++i){ cout<<"main fun index:"<<i<<endl; tpl->commit(fun1,i * 100);// std::this_thread::sleep_for(std::chrono::milliseconds(20)); } cout<<"count:"<<count<<endl; return 0;}

一种输出为:

15:32:05: Starting E:\gitfile\cpp_demo\build-threadpool_demo-cmake_kit-Debug\threadpool_demo.exe ...main----0x41b060thread pool size:16main fun index:0main fun index:1main fun index:2main fun index:3main fun index:4main fun index:5fun1 id:4 sleep ms:0main fun index:fun1 id:fun1 id:fun1 id:162fun1 id:56 sleep ms:6300 sleep ms:400
sleep ms: sleep ms:200100fun1 id:
fun1 id:main fun index:77 sleep ms:12500 sleep ms:
600main fun index:8fun1 id:14 sleep ms:700fun1 id:main fun index:912 sleep ms:800count:9fun1 id:4 sleep ms:90015:32:05: E:\gitfile\cpp_demo\build-threadpool_demo-cmake_kit-Debug\threadpool_demo.exe exited with code 0