对象从容器中删除它自己 [英] Object delete itself from container

查看:125
本文介绍了对象从容器中删除它自己的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个容器(任何种类,可能是std :: map或std :: vector)包含一个类的对象与一些网络东西运行在一个线程,检查它是否仍然连接(线程被定义在内部该类和构造时启动)。



有没有任何方法,我可以使对象从容器中删除它自己断开连接或应该移动线程外的对象,使用那个类只存储数据?

解决方案

我会有卸载队列。



当线程注意到连接已关闭时,它注册对象(和连接器)与卸载队列尽可能多的一切,然后thred终止。 p>

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


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?

解决方案

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).

这篇关于对象从容器中删除它自己的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆