progschj / ThreadPool

A simple C++11 Thread Pool implementation
zlib License
7.85k stars 2.24k forks source link

Fix a bug by change the queue store shared_ptr. #25

Closed RonTang closed 9 years ago

RonTang commented 9 years ago

1.Modify the queue storing shared_ptr,because move construct a shared_prt never throw exception that avoid some thread throw exception to terminate process. 2.Add a function wrapper that make some code clearly.I spend a long time to understand the code tasks.emplace([task](){ (*task)(); });

johnlanni commented 9 years ago

你好,tasks.emplace([task]{ (*task)(); });这段的意思我理解是,lambada是通过伪函数实现的,那么task这个shared_ptr对象被lambada捕获以后是copy了一份在一个伪函数类对象里,所以用队首的task移动构造时,这个对象里面的成员也只有一个shared_ptr而已,那么移动构造不也是不会抛出任何异常吗? 抱歉,我的英语不好,这里直接用中文回复你了...

RonTang commented 9 years ago

你好,你理解是对的。我认真看了下,确实task不是一个packaged_task,而是一个shared_ptr。把变量名task改为task_ptr应该更好一些。这种实现方式很节省代码,但也确实比较绕。谢谢你的提醒。