Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.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++中的多线程服务器,如何终止线程并进行良好的清理 我在C++服务器上编写的服务器像代理一样工作。主要功能: try { Connector c(ip); //establishes persistent connection to the server B Listener1 l1(port); //listens incoming connection to the server A (our proxy) and push them into the queue Listener2 l2(&c); //listens responses (also push messages) from the server B and push them into the queue Proxy p(&c, &l1, &l2); //pulls clients out from the queue and forwards requests to the server B, pull out everything from the listener2 queue and returns as a responce KeepAlive k(&l1, &p); //pushes the empty client to the listeners1's queue thus the proxy sends keepalive to the server B and the response is discarded l1.start(); p.start(); l2.start(); k.start(); l1.join(); p.join(); l2.join(); k.join(); catch(std::string e) { std::cerr << "Error: " << e << std::endl; return -1; }_C++_Multithreading_Sockets_Openssl_Thread Safety - Fatal编程技术网

C++中的多线程服务器,如何终止线程并进行良好的清理 我在C++服务器上编写的服务器像代理一样工作。主要功能: try { Connector c(ip); //establishes persistent connection to the server B Listener1 l1(port); //listens incoming connection to the server A (our proxy) and push them into the queue Listener2 l2(&c); //listens responses (also push messages) from the server B and push them into the queue Proxy p(&c, &l1, &l2); //pulls clients out from the queue and forwards requests to the server B, pull out everything from the listener2 queue and returns as a responce KeepAlive k(&l1, &p); //pushes the empty client to the listeners1's queue thus the proxy sends keepalive to the server B and the response is discarded l1.start(); p.start(); l2.start(); k.start(); l1.join(); p.join(); l2.join(); k.join(); catch(std::string e) { std::cerr << "Error: " << e << std::endl; return -1; }

C++中的多线程服务器,如何终止线程并进行良好的清理 我在C++服务器上编写的服务器像代理一样工作。主要功能: try { Connector c(ip); //establishes persistent connection to the server B Listener1 l1(port); //listens incoming connection to the server A (our proxy) and push them into the queue Listener2 l2(&c); //listens responses (also push messages) from the server B and push them into the queue Proxy p(&c, &l1, &l2); //pulls clients out from the queue and forwards requests to the server B, pull out everything from the listener2 queue and returns as a responce KeepAlive k(&l1, &p); //pushes the empty client to the listeners1's queue thus the proxy sends keepalive to the server B and the response is discarded l1.start(); p.start(); l2.start(); k.start(); l1.join(); p.join(); l2.join(); k.join(); catch(std::string e) { std::cerr << "Error: " << e << std::endl; return -1; },c++,multithreading,sockets,openssl,thread-safety,C++,Multithreading,Sockets,Openssl,Thread Safety,目前,我有以下问题/疑问: **1.**我从构造函数抛出一个异常,这是一个好的实践吗?当无法建立连接时,我抛出一个异常,我想这就是为什么不应该创建对象的原因 **2.**在连接超时或服务器B关闭连接等情况下,关闭应用程序安全和清理存在问题。listener1和listener2使用阻塞函数系统调用accept和BIO_从openssl库读取,因此不可能仅从另一个线程设置循环条件。问题还在于,所有模块都是通过互斥体连接和共享资源的。我当前的代码只是调用exit函数来终止整个应用程序 我知道这不是一

目前,我有以下问题/疑问:

**1.**我从构造函数抛出一个异常,这是一个好的实践吗?当无法建立连接时,我抛出一个异常,我想这就是为什么不应该创建对象的原因

**2.**在连接超时或服务器B关闭连接等情况下,关闭应用程序安全和清理存在问题。listener1和listener2使用阻塞函数系统调用accept和BIO_从openssl库读取,因此不可能仅从另一个线程设置循环条件。问题还在于,所有模块都是通过互斥体连接和共享资源的。我当前的代码只是调用exit函数来终止整个应用程序

我知道这不是一个完美的解决方案,我感谢任何建议和提示


提前感谢

如果异常失败,构造函数应该抛出异常。C++是用来处理这个问题的。基类和成员已被构造时才会被清理

阻止来自其他库的函数总是一个问题。Windows和POSIX处理得很好:WSAWaitForMultipleObjectEx和select允许您添加一个额外的句柄,您可以使用该句柄取消阻止等待

在accept调用中,您可以通过从主线程通过localhost创建连接来假装这一点。检测到此异常连接将是停止接受进一步连接的信号

至于openSSL读取,我只需从主线程关闭套接字,线程安全性就糟了。我会确保我在关机后很晚才这么做,我也不希望这个库在那之后可以使用