对象从容器中删除自身

发布于 2024-07-19 08:53:48 字数 173 浏览 7 评论 0原文

所以我有一个容器(任何类型,可能是 std::map 或 std::vector),其中包含一个类的对象,其中一些网络事物在一个线程中运行,该线程检查它是否仍然连接(该线程是在该类中定义的,并且构建时启动)。

有什么方法可以让对象在断开连接时从容器中删除自身,或者我应该将线程移到对象之外并使用该类来存储数据吗?

So I have a container(any kind, probably std::map or std::vector) which contains objects of a class with some network thing running in a thread that checks if it is still connected (the thread is defined inside that class and launches when constructed).

Is there any way I can make the object delete itself from the container when its disconnected or should I move the thread outside the object and use that class just to store data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

预谋 2024-07-26 08:53:48

为了使对象从容器中删除自身,它必须知道它位于哪个容器中。您需要在对象中维护一个指向容器的指针。 您还必须使用锁来保护容器,以阻止多个线程同时访问容器。

我想我更喜欢你的第二个解决方案 - 一些管理对象在从集合中删除死对象后进行查找。 如果不出意外的话,这将更容易调试,并且锁定逻辑将集中在单个对象中。

In order for the object to delete itself from the container, it will have to know which container it is in. You will need to maintain a pointer to the container in the object. You will also have to protect the container with a lock to stop multiple threads accessing the container at the same time.

I think I prefer your second solution - some managing object looks after removing dead objects from the collection. If nothing else, this will be quite a bit easier to debug and the locking logic becomes centralised in a single object.

走走停停 2024-07-26 08:53:48

我会有卸载队列。

当线程注意到连接已关闭时,它会将对象(和容器)注册到卸载队列中,尽可能地整理所有内容,然后线程终止。

然后,卸载队列内有一个单独的线程。 它的唯一目的是监视队列。 当它在队列中看到新对象时,将其从容器中删除,然后销毁它(根据需要与对象线程同步)。

I would have am unload queue.

When a thread notices that the connection is down it registers the object (and continer) with the unload queue tides everything up as much as possible then the thred terminates.

A separate thread is then inside the unload queue. Its sole purpose is to monitor the queue. When it sees a new object on the queue, remove it from the container and then destroy it (syncing with the objects thread as required).

无声无音无过去 2024-07-26 08:53:48

STL 容器倾向于假设它们正在存储值; 可以复制且副本相同的对象。 通常,具有线程的对象不太适合该模型。 他们有更强烈的认同感。 在这种情况下,您肯定具有同一性 - 容器中对象的副本与外部的副本不同。

STL containers tend to assume they're storing values; objects that can be copied and where copies are identical. Typically, objects which have threads fit poorly into that model. They have a much stronger sense of identity. In this case, you definitely have indentity - a copy of the object in a container is distinct from a copy outside.

清眉祭 2024-07-26 08:53:48

我遇到了与您的问题非常相似的问题,我通过在检测到断开连接时从“网络事物”发出 boost::signal 来解决该问题,并被管理容器的对象捕获。 收到该信号后,它将遍历容器,从中删除无效的网络会话。 可能值得在这里查看:

如何从封装了发出该信号的对象的对象中捕获 C++ boost::signal?

干杯,

Claudio

I had a problem very similar to yours, which I solved by emitting a boost::signal from the "network thing" when it detected the disconnection, being caught by the object managing the container. Upon receiving that signal, it would iterate through the container, removing the dead network session from it. It might be worth looking at it here:

How to make a C++ boost::signal be caught from an object which encapsulates the object which emits it?

Cheers,

Claudio

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文