【发布时间】:2020-08-09 07:44:51
【问题描述】:
我正在尝试学习 io_service 并使用共享指针。我希望代码可以无限工作,直到我调用 stop 方法或类似的东西。不幸的是,在屏幕上看到 workHandler 的输出后,程序关闭了。谁能解释为什么会这样?
#include <boost/asio.hpp>
#include <iostream>
#include <atomic>
#include <memory>
#include <thread>
#include <vector>
class Service : public std::enable_shared_from_this<Service> {
std::shared_ptr<boost::asio::io_service> _service;
std::shared_ptr<boost::asio::io_service::work> _work;
std::vector<std::thread> _threads;
std::atomic<bool> _started{false};
public:
Service()
: _service(std::make_shared<boost::asio::io_service>()),
_work(std::make_shared<boost::asio::io_service::work>(*_service))
{}
void start() {
auto self(this->shared_from_this());
auto startHandler = [this, self]() {
std::cout << "StartHandler\n";
while(!_started) _service->run();
};
_threads.emplace_back(std::thread(startHandler));
}
std::shared_ptr<boost::asio::io_service>& get() { return _service; }
};
class Worker : public std::enable_shared_from_this<Worker> {
std::shared_ptr<Service> _service;
std::shared_ptr<boost::asio::io_service> _io_service;
public:
Worker(const std::shared_ptr<Service>& service)
: _service(service),
_io_service(_service->get())
{}
void work() {
auto self(this->shared_from_this());
auto workHandler = [this, self]() {
std::cout << "WorkHandler\n";
};
_io_service->post(workHandler);
}
};
int main() {
auto ser = std::make_shared<Service>();
ser->start();
auto worker = std::make_shared<Worker>(ser);
worker->work();
}
【问题讨论】:
-
我已经调试了代码,不幸的是我不知道它为什么会退出。据我了解,io_service 应该始终感谢 io_service::work 对象。在这里它在 workHandler 之后结束,所以这就是我提出问题的原因。
-
代码执行您编写的操作。您创建对象,然后它们在范围结束时被销毁。为什么你认为它应该在退出之前停止?你从不加入的 std::thread 也会通过及时调用 std::terminate 来提供帮助。
-
那么捕获的 shared_from_this 应该让它保持活力,@rustyx,这可能就是问题的重点。
标签: c++ multithreading c++11 c++14 boost-asio