Boost 线程在作业完成之前就终止了
我正在使用 boost 库来实现套接字通信。对于我的主应用程序,应该启动一个连接处理程序来处理所有传入的请求。
因此,我将整个服务器处理程序封装到类服务器中。创建服务器对象后,它应该启动服务器。
然而,这样线程就会随着构造函数代码的执行结束而死亡。我想我不明白 boost / posix 线程是如何工作的。我有 Java 背景。
server::server(int port) {
try {
boost::asio::io_service io_service;
tcp_server server(io_service, port);
boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
}
I am using the boost library to implement a socket communication. In respect to my main application, a connection handler should be launched who deals with all incoming requests.
Therefore I have encapsulated the whole server handler into the class server. When the server object is created it should launch the server.
However this way the thread dies with the execution end of the constructor code. I guess I don't get how boost / posix threads work. I come from a Java background.
server::server(int port) {
try {
boost::asio::io_service io_service;
tcp_server server(io_service, port);
boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在构造一个局部变量 io_service ,并将其传递给新线程的辅助函数。当变量超出范围(构造函数退出)时,
io_service
将被销毁并且无法再访问。但是,您的线程的工作函数不知道这一点,并且可能会尝试再次访问该对象的剩余部分。丑陋随之而来。
该错误与线程无关,而是称为“
解决方案是通过手动控制(
new
/delete
)或增加它(例如使其成为服务器上的类成员,而不是构造函数内的本地成员)。您似乎还对
server
变量有问题,该变量也是本地变量,一旦构造函数存在就会被销毁。由于您实际上根本没有使用它,这不是您发布的代码的问题,但无论如何,这表明您正在做一些可疑的事情。You are constructing a local variable
io_service
, which you pass to the worker function of your new thread. When the variable goes out of scope (constructor exits),io_service
is destroyed and can no longer be accessed.However, your thread's worker function does not know this and presumably tries to access the remains of that object again. Ugliness ensues.
The error does not have to do with threads but is an instance of a common type of error called "returning the address of a local" (even though you are not actually returning it here, the mechanism is the same).
The solution would be to extend the lifetime of the
io_service
object, either by taking manual control (new
/delete
) or by increasing it (e.g. making it a class member onserver
instead of a local inside the constructor).You also seem to have a problem with the
server
variable, which is also a local and will be destroyed as soon as the constructor exists. Since you aren't actually using it at all this is not a problem with the code you posted, but anyway it's an indication that you are doing something suspicious.您的
io_service
需要显式地或通过您的tcp_server
对象来准备一些要执行的io_service::work
任务。否则 io_service::run() 将立即返回控制权。研究asio示例,这个概念是重要的是要理解。我怀疑您的代码是一个人为的示例,它可能不需要线程来运行 io_service。主线程就可以了。
Your
io_service
needs to be primed with someio_service::work
to do, either explicitly or from yourtcp_server
object. Otherwiseio_service::run
() will return control immediately. Study the asio examples, this concept is important to understand.I suspect your code is a contrived example, it likely does not need a thread to run the io_service. The main thread will do just fine.