Я использую символ в области видимости и привязываю его к ThreadPool
для выполнения задачи, как показано ниже:
void test_thread_pool_arr(){
auto lb_print = [](const char* str){
info("print msg: %s, and i will sleep 2s", str);
sleep(2);
};
ThreadPool tp(3);
{
char arr[10];
memset(arr, '1', sizeof arr);
arr[9] = '\0';
tp.addTask(std::bind(lb_print, arr));
tp.addTask(std::bind(lb_print, arr));
} // leave scope so, arr should be invalid
info("leave scope....");
tp.exit(); // tell ThreadPool to join and then exit.
tp.join();
info("ending....");
}
Вывод, как показано ниже:
2018/12/06-20:07:59 leave scope....
2018/12/06-20:07:59 print msg: 111111111, and i will sleep 2s
2018/12/06-20:07:59 print msg: 111111111, and i will sleep 2s
2018/12/06-20:08:01 ending....
Я думаю, что когда arr покидает свою область, он должен быть уничтожен, но выводится нормально (целевая функция lb_print просто получает адрес arr, но arr уничтожается).
Почему?Std :: bind продлевает это время жизни?Если да, то как это работает?
Другой пример с объектом класса:
class BindScope{
public:
BindScope(int i=0):n_(i){
info("BindScope ctor %d", n_);
}
~BindScope(){
info("BindScope ~dtor %d", n_);
}
void print()const{
info("do print %d", n_);
}
int n_;
};
void test_thread_pool_scope(){
auto lb_print = [](const BindScope& bs){
bs.print();
};
ThreadPool tp(3);
{
BindScope bs(4);
tp.addTask(std::bind(lb_print, std::ref(bs)));
tp.addTask(std::bind(lb_print, std::ref(bs)));
}// bs be destoryed, but tp do task normally
info("out scope");
tp.exit();
tp.join();
info("ending.........");
}
Вывод, как показано ниже:
2018/12/06-20:14:03 BindScope ctor 4
2018/12/06-20:14:03 BindScope ~dtor 4
2018/12/06-20:14:03 out scope
2018/12/06-20:14:03 do print 4
2018/12/06-20:14:03 do print 4
2018/12/06-20:14:03 ending.........
Мы можем видеть, что когда объект bs
выходит из области видимости, его уничтожают, но ThreadPool
выполняет задачу в обычном режиме.Почему?