chenshuo / muduo

Event-driven network library for multi-threaded Linux server in C++11
https://github.com/chenshuo/muduo
Other
14.7k stars 5.15k forks source link

在非主线程创建Server #531

Closed qazwsxedckll closed 2 years ago

qazwsxedckll commented 2 years ago

我现在在实现自动化测试的harness,需要创建多个TcpClient和TcpServer,主线程控制TestCase。如果TcpServer在主线程,传入EventloopThread的loop的话,析构时会因为不在同一个线程而报错。如果loop在主线程,loop.loop()会导致失去对test的控制。那个java实现我也看过了,没有研究过netty库,backend好像不存在这个问题。请问这种情况有什么好的解决方法吗。

chenshuo commented 2 years ago

如果在启动 EventLoopThread 的时候就知道有哪些 TcpServer 在这个线程,你可以试试 EventLoopThread 的 ThreadInitCallback 功能。 如果在启动 EventLoopThread 的时候不知道要创建多少 TcpServer ,可以用 EventLoopThread::startLoop()->RunInLoop() 来创建 TcpServer。

qazwsxedckll commented 2 years ago

主要是析构的问题,我用runInLoop([&](){delete tcpserver})解决了,谢谢了。