TcpServer和EventLoop该如何正确析构 #597
Jasons0215
started this conversation in
General
Replies: 1 comment
-
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
你好
问题1:
我看muduo得example里TcpServer和EventLoop都是在主线程里创建并执行,但是在我实际得业务里,有自己得业务循环,所以我将TcpServer和EventLoop放在子线程里去创建和loop,但是程序退出时,会提升和创建得线程不一致导致crash。
问题2:
是否TcpServer得对象需要先于EventLoop对象析构,不然TcpServer里得EventLoop对象就失效了
期待您得回复!!
创建子线程
int Gate::Init(const std::string& conf)
{
net_thread_ = std::thread(&Gate::StartNetLoop, this);
}
在子线程:
void Gate::StartNetLoop()
{
muduo::net::InetAddress serverAddr(tcp_port_);
event_loop_ = std::make_sharedmuduo::net::EventLoop();
im_net_server_ = std::make_shared(event_loop_.get(), serverAddr, channel_);
im_net_server_->setThreadNum(net_thread_count_);
im_net_server_->start();
event_loop_->loop();
}
在收到kill消息时退出loop
int Gate::Finish()
{
event_loop_->runInLoop(std::bind(&muduo::net::EventLoop::quit, event_loop_.get()));
net_thread_.join();
return 0;
}
Beta Was this translation helpful? Give feedback.
All reactions