退出c+中具有三个线程的两个并发队列+; 我退出多线程、多队列C++程序时遇到了问题。该图显示了队列和线程结构。图表如下:
简而言之,我有三个线程和两个并发队列。第二个_处理程序(第二个_线程)从第一个队列弹出并推送到第二个队列。一切(似乎)都很好,直到我想按键盘键退出程序。我得到这个错误: 在抛出“boost::exception\u detail::clone\u impl>的实例后调用terminate what():boost::lock\u错误 流产 这是我的密码: 主要退出c+中具有三个线程的两个并发队列+; 我退出多线程、多队列C++程序时遇到了问题。该图显示了队列和线程结构。图表如下:,c++,multithreading,boost,concurrency,queue,C++,Multithreading,Boost,Concurrency,Queue,简而言之,我有三个线程和两个并发队列。第二个_处理程序(第二个_线程)从第一个队列弹出并推送到第二个队列。一切(似乎)都很好,直到我想按键盘键退出程序。我得到这个错误: 在抛出“boost::exception\u detail::clone\u impl>的实例后调用terminate what():boost::lock\u错误 流产 这是我的密码: 主要 intmain(){ startMultiThreading(); 首先,请参阅KillianDS的评论-您的示例太长了 另一件事是:不
intmain(){
startMultiThreading();
首先,请参阅KillianDS的评论-您的示例太长了
另一件事是:不要直接调用析构函数!!
析构函数是一种特殊的语言,Garantues会在变量作用域的末尾调用它。如果您手动调用它,它将再次被调用,这很可能会导致未定义的行为
这是一个庞大的代码量,请将其缩减为较小的代码量。另外:当您点击abort命令时引发abort异常有什么不对?感谢您的评论KillianDS。感谢KillianDS和G.Martinek的回复。关于析构函数和异常,我学到了两个明智的教训。我没有成功获得cout在主要印刷品中。但可能没有必要。
int main() {
startMultiThreading();
cout <<"I"<<endl;
}
void startMultiThreading() {
boost::thread_group someVar_workers;
boost::thread_group someOtherVar_workers;
concurrent_queue<someVar* > someVar_queue(&someVar_workers);
concurrent_queue<someOtherVar*> someOtherVar_queue(&someOtherVar_workers);
boost::thread *first_thread = new boost::thread(first_handler, &someVar_queue);
boost::thread *second_thread = new boost::thread(second_handler, &someVar_queue, &someOtherVar_queue);
boost::thread *third_thread = new boost::thread(third_handler, &someOtherVar_queue);
someVar_workers.add_thread(first_thread);
someVar_workers.add_thread(second_thread);
someOtherVar_workers.add_thread(second_thread);
someOtherVar_workers.add_thread(third_thread);
while (true) {
if (thread_should_exit) {
cout << "threads should be killed" << endl;
while (!someVar_queue.empty()) {
usleep(1000);
}
someVar_workers.remove_thread(second_thread);
while (!someOtherVar_queue.empty()) {
usleep(1000);
}
someOtherVar_queue.cancel();
someVar_workers.join_all();
someOtherVar_workers.remove_thread(second_thread);
someOtherVar_workers.join_all();
break;
}
usleep(10000);
}
cout << "H" << endl;
}
End of first_handler
threads should be
second_handler is canceled
End of second_handler
H
terminate called after throwing an instance of 'concurrent_queue<someOtherVar*>::Canceled'
Aborted
Press [Enter] to close the terminal ...