gpt4 book ai didi

c++ - 如何在Boost asio多线程服务器的单线程中取消所有异步操作?

转载 作者:行者123 更新时间:2023-12-02 10:36:30 26 4
gpt4 key购买 nike

我想实现服务器的单线程终止,允许继续执行
其他线程的操作(接受,读取,写入)。
我应该为每个线程使用自己的受体对象吗?
是否存在为此的api功能,或者我应该实现套接字的寄存器,我将在哪里标记哪个套接字属于线程?
目前,我实现了该线程的循环:

void TServer::Process_connections(IThread* _th, TThcode _idx)
{
const char * FUNCTION = __FUNCTION__;
using namespace boost::asio::ip;
using namespace boost::asio;
error_code ec;
LOG + "s: server thread loop is started" << std::to_string(_idx);
//auto m_pthread = m_pmngthread.get();
auto isRunning = _th->isRunning();//m_pthread->isRunning();
while(isRunning)
{
if ((m_state(state::running)))
{
do_accept(_idx);
//m_state << state::run;
break;
}
isRunning = _th->isRunning();
}
while(isRunning)
{
if (m_state(state::run))
{
try
{
m_ioservice.poll(ec);
} catch (...)
{
errspace::show_errmsg(FUNCTION);
m_state << state::shutdowned;
}
if (ec) break;
clearGarbage();
}

isRunning = _th->isRunning();//m_pthread->isRunning();
}
LOG << "s: server thread loop is stopping";
// only for single thread server
m_acceptor.cancel(ec);
if (ec)
{
errspace::show_errmsg("error in acceptor::cancel");
}
// only for single thread server
m_ioservice.run(ec);
if (ec)
{
errspace::show_errmsg("error in ioservice::run");
}
std::unique_lock<std::recursive_mutex> lock(m_mtx_garbage);
std::unique_lock<std::recursive_mutex> lock2(m_mtx_active);
m_socketset_garbage.clear();
m_socketset_active.clear();
// only for single thread server
m_acceptor.close();
if (ec)
{
errspace::show_errmsg("error in acceptor::close");
}
LOG + "s: loop is stopped" << std::to_string(_idx);
}

最佳答案

只有取消才意味着停止:io_context::stop

关于c++ - 如何在Boost asio多线程服务器的单线程中取消所有异步操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60048002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com