c++中的多线程服务器,如何很好地终止线程和清理
Multithread server in C++, how to terminate threads and clean up nicely
我用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;
}
目前我有以下问题/疑问:
* * 1。**我抛出一个异常从构造函数,这是好的做法吗?当无法建立连接时抛出异常,我猜这就是为什么不应该创建对象的原因。
* * 2。**当连接超时或服务器B关闭连接等时,关闭应用程序安全和清理存在问题。listener1和listener2使用阻塞函数(系统调用accept()和来自openssl lib的BIO_read),因此不可能仅从另一个线程设置循环条件。问题还在于所有模块都是连接的,并且使用互斥锁共享资源。我当前的代码只是调用exit函数来终止整个应用程序。
我知道这不是一个完美的解决方案,我感谢任何建议和提示。
Thanks in advance
构造函数失败时应该抛出异常。c++就是为了很好地处理这个问题而设计的。当且仅当基类和成员已经构造时,它们才被清理。
阻塞来自其他库的函数总是一个问题。Windows和POSIX处理得很好:WSAWaitForMultipleObjectEx
和select
允许你添加一个额外的句柄,你可以用它来解除等待阻塞。
在accept
调用中,您可以通过从主线程通过localhost创建连接来实现此操作。检测到这个"异常"连接将是停止接受进一步连接的信号。
至于openSSL的读取,我只会从主线程关闭套接字,线程安全是该死的。我会确保在关闭的很晚的时候这样做,并且我不期望库在那个时间点之后还能使用。
相关文章:
- 我编写了代码将十进制分数转换为其二进制等效数.它编译得很好,但在执行时挂起
- 很好的语法来获取对向量/数组数据的大小引用?
- 我认为我的代码很好,但它在 cin a 之后停止并且没有进一步?
- 尽管一切看起来都很好,但值不会交换
- 为什么不同类型层次结构的指针之间的dynamic_cast定义得很好?
- 在 C++11 中利用 int*_t、int_fast*_t 和 int_least*_t 之间的差异的一个很好的例子是
- 对于短字符串来说,这是一个很好的哈希函数吗?
- 为什么指标有时效果很好,有时效果不佳?写下霍夫曼代码
- Red Hat:使用<atomic>编译很好,但链接器找不到__atomic_store_16;什么库?
- 使用移位的无符号数字作为数组的索引号是一种很好的做法
- 是否很好地使用状态模式来维护当前选定的对象?
- C++|以一种很好的方式将树(不一定是二进制的)打印到stdout
- 静态库单独使用很好,但在引用时会抛出错误
- 如果验证容器的大小并在同一条件语句下访问元素,这是很好的做法吗?
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 有没有办法关闭文件? fclose 不能很好地工作
- 引用的静态强制转换强制模板实例化,其中不完整的类型很好
- libc++:为什么流关闭后仍然很好
- 不能很好地运行HElib,但它建立在Windows 10 x64上
- c++中的多线程服务器,如何很好地终止线程和清理