gpt4 book ai didi

c++ - 如何正确关闭助推器线程

转载 作者:行者123 更新时间:2023-12-02 10:13:11 25 4
gpt4 key购买 nike

在我的代码中,我打开了一个用于io服务的boost线程,并希望在while循环结束后关闭该线程。
目前,应用程序在 Debug模式下崩溃,并且我收到此错误消息。

Microsoft C++ exception
boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> > at memory location 0x000000000AA2F7D0.


Microsoft C++ exception:
boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> > at memory location 0x000000000AA2F7D0.

template<class F>
struct Cleaner {
Cleaner(F in) : f(in) {}
~Cleaner() { f(); }
F f;
};

template<class F>
Cleaner<F> makeCleaner(F f) {
return Cleaner<F>(f);
}


int main()
{
boost::asio::io_service io_service;
server server1(io_service, 1980);
boost::thread t(boost::bind(&io_service::run, &io_service));

while( loop )
{



}
auto raii = makeCleaner([&]() { io_service.stop(); }); // trying to close the boost thread

}

最佳答案

根据评论,仅调用io_service.stop()是不够的。运行该服务的线程必须为 joined 才能正常退出。

关于c++ - 如何正确关闭助推器线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62752051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com