删除矢量内指针的适当方法
Appropriate way to delete pointers within a vector
我有一个多线程聊天服务器,主服务器类有一个客户端的 std::vector。每个客户端都在自己的线程上运行,它们持有指向客户端向量的指针,以便它们可以相互通信。
我不确定的是,当客户端断开连接时,从此向量中删除对象的最佳方法是什么?
在这种情况下,是否可以只使用互斥锁锁定资源,然后允许它从客户端向量中删除/删除自身?
或者,拥有客户端向量的服务器对象最好执行某种检查以查看该连接是否已关闭,然后让该类管理资源的释放?
我对智能指针解决方案不感兴趣,这是一个学习项目。
遵循关注点分离的原则,从设计的角度来看,如果服务器在线程中启动客户端,我建议在服务器类中管理所有这些东西。
如果连接丢失,客户端线程可以通知服务器线程,然后服务器对象可以执行必要的操作以将客户端从其监视列表中删除并删除它。
一般的想法是这样的:
class Server;
class Client {
Server* myserver; // edit: sorry I forgot the backreference
clientid_t id; // unique id, provided by the server at construction
public:
clientid_t get_id(); // for server when searching for a specific client
... // when connection is lost, tell the server
... // the destructor will do everything needed to clean up/close a chat
};
class Server {
vector<Client*> chats; // Or better, a smart pointer
mutex critical; // used when accessing to the chats container.
public:
void listen_for_new_chats(); // create new chats and insert them in container
void close_chat (clientid_t id); // called when chat lost connection (abort)
// this function shall remove the chat from container and destroy client
...
};
这使您可以完全控制服务器独立于客户端发展:
- 如果有一天你选择队列或列表而不是Vector,客户将不需要知道。
- 客户端不需要知道服务器如何管理客户端(直接分配、智能指针或直接作为容器中的对象(。
从并发的角度来看,这种封装也将使其更加可靠。 不仅在删除客户端时应锁定矢量,还应在访问它以进行读取/搜索时锁定矢量。 通过将向量保留为服务器的私有,可以确保永远不会通过放弃锁而无意中使用向量。
相关文章:
- 将一个类的方法指针存储到另一个类中
- 如何调用返回类方法指针的类方法
- 通过reinterpret_casting方法指针从指针调用派生类的方法。这是 UB 吗?
- 泛型方法指针.reinterpret_cast指向不同类的方法指针,这是 UB 吗?
- C++;类方法指针;λ;将 lambda 作为成员函数指针传递;
- 使用模板、方法指针和字符串键入推导
- 将方法指针作为整数参数发送到C#的C 方法
- 从C 中的VTable获取方法指针
- 模板化方法指针 - 无法匹配函数参数的指针
- 获取特定的模板重载方法指针
- 如何将方法指针声明为Typedef方法参数
- 如何将方法指针类型转换为函数指针类型
- 如何在使用 pthreads 时将方法指针作为函数参数传递C++
- 方法指针映射,编译器说他们不接受任何参数
- C# 方法指针,如 C++ 中的指针
- c++创建新的LinkedList类:Clear()方法指针被释放未分配
- 尝试获取类方法指针时出现E_NOINTERFACE
- 传递和强制转换方法指针
- 常量方法指针的类型是什么
- 将对象的方法指针传递给接受 [静态方法指针/全局函数] 指针的函数