用于跟踪活动连接的数据结构
当我想跟踪活动连接时,将它们保存到链表中还是直接保存到索引代表连接 ID 的某个数组中更好?我想防止可能的竞争条件问题,例如:
- 列表中的连接数量很大,
- 有人开始搜索位于列表末尾的连接
- ,同时在搜索过程中,连接被分离
When I want to keep track of active connections, is it better to save them into linked list or directly to some array where index will represent ID of the connection? I want to prevent possible race condition issues, for example:
- number of connections in the list is big
- someone start to search for a connection which is at the end of the list
- meanwhile, during the search, the connection is detached
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该结构应该有一个连接句柄(SOCKET)、一个引用计数和一个表示应关闭连接并尽快删除对象的标志。无论容器是什么,它都必须具有用于搜索/插入/删除以及 GetReference/Release 逻辑的同步机制(关键部分)。如果引发关闭标志,GetReference 函数应返回 NULL。您可能需要多个 GetReference,具体取决于搜索条件(并且连接对象应该具有可以帮助在容器中找到它的所有这些值)。如果容器中不存在,GetReference 函数之一可以创建新对象。
如果引发关闭标志并且引用计数降至零,释放函数应关闭连接并从容器中删除对象。但是,发布不得关闭关键部分内的连接,因为这可能是耗时的操作(取决于正常关闭和延迟选项)。 Release应该进入CS,减少引用计数,如果为零则离开CS,关闭连接,再次进入CS,并从容器中移除对象并删除它。由于关闭标志已升高,因此两个 CS 之间的引用计数不会升高。
The structure should have a connection handle (SOCKET), a reference count, and a flag denoting that connection should be closed and the object deleted as soon as possible. Whatever the container is it must have synchronization mechanism (critical section) for search/insert/remove, and GetReference/Release logic. GetReference function should return NULL if closing flag is raised. You might need more that one GetReference, depending on search conditions (and connection object should have all those values that can help it being found in container). One of GetReference functions can create new object if it does not exist in container.
Release function should close the connection and delete the object from container if closing flag is raised and reference count is dropped to zero. However Release must not close the connection within critical section, because that can be time consuming operation (depends on graceful shutdown and lingering option). Release should enter CS, decrease reference count, if it is zero then leave CS, close connection, enter CS again, and remove object from container and delete it. Because the closing flag is raised the reference count will not raise between two CS's.