Boost 线程在作业完成之前就终止了

发布于 2024-10-26 09:19:15 字数 503 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝對不後悔。 2024-11-02 09:19:16

您正在构造一个局部变量 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 on server 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.

梦里的微风 2024-11-02 09:19:16

您的 io_service 需要显式地或通过您的 tcp_server 对象来准备一些要执行的 io_service::work 任务。否则 io_service::run() 将立即返回控制权。研究asio示例,这个概念是重要的是要理解。

工作类用于通知
io_service 工作开始时
完成。这确保了
io_service 对象的 run() 函数
工作进行时不会退出,
并且当没有时它确实退出
剩下未完成的工作。

工作类是可复制构造的
以便它可以用作数据
处理程序类中的成员。它不是
可分配。

我怀疑您的代码是一个人为的示例,它可能不需要线程来运行 io_service。主线程就可以了。

Your io_service needs to be primed with some io_service::work to do, either explicitly or from your tcp_server object. Otherwise io_service::run() will return control immediately. Study the asio examples, this concept is important to understand.

The work class is used to inform the
io_service when work starts and
finishes. This ensures that the
io_service object's run() function
will not exit while work is underway,
and that it does exit when there is no
unfinished work remaining.

The work class is copy-constructible
so that it may be used as a data
member in a handler class. It is not
assignable.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文