Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从另一个线程中断websocket(使用boost beast)?_C++_Boost_Websocket_Boost Beast - Fatal编程技术网

C++ 如何从另一个线程中断websocket(使用boost beast)?

C++ 如何从另一个线程中断websocket(使用boost beast)?,c++,boost,websocket,boost-beast,C++,Boost,Websocket,Boost Beast,我正在使用BoostBeast 1.74.0。在另一个线程中,我尝试关闭websocket,但代码在“acceptor.accept(socket,endpoint)”处被破坏,在调用关闭后我收到了“Signal:SIG32(Real-timeevent 32)” 从代码部分到监听连接,我需要做什么更改才能中断并正确接受服务 ... _acceptor = &acceptor; _keepAlive = true; while (_keepAlive) {

我正在使用BoostBeast 1.74.0。在另一个线程中,我尝试关闭websocket,但代码在“acceptor.accept(socket,endpoint)”处被破坏,在调用关闭后我收到了“Signal:SIG32(Real-timeevent 32)”

从代码部分到监听连接,我需要做什么更改才能中断并正确接受服务

...
 _acceptor = &acceptor;
 _keepAlive = true;
 while (_keepAlive) {
            tcp::socket socket{ioc};
            // Block until we get a connection
            acceptor.accept(socket, endpoint);

            // Launch the session, transferring ownership of the socket
            std::thread(
                    &WebSocketServer::doSession,
                    std::move(socket),
                    this,
                    this,
                    getHeaderServer()
            ).detach();
        }
关闭另一个线程的函数调用

void WebSocketServer::close() {
    if (_acceptor != nullptr) this->close();
    _keepAlive = false;
}

glibc
使用SIG32发出取消使用pthread库创建的线程的信号。您正在尝试使用
pthread\u kill

如果没有,你可能只是因为你在GDB下运行它才看到这一点。这应该可以通过告诉GDB忽略以下内容来解决:

handle SIG32 nostop noprint
最后是原问题:

  • 在Boost线程中有一些中断点。它们可以帮助您iff您可以切换到Boost-Thread
    Boost::Thread
    而不是
    std::Thread
    。此外,您必须更改线程的代码以实际检查中断:

  • 既然听起来您确实想要终止accept循环,为什么不“简单地”取消acceptor呢?我不完全确定这是否适用于同步操作,但您当然可以轻松地使用异步accept

    注意同步对接受者对象本身的访问。这意味着要么在同一线程上运行
    cancel
    ,执行
    async\u accept
    ,要么从同一
    线程运行
    cancel
    。从这一点上看,异步完成整个过程似乎更容易