如何通过仅终止子对象的父对象来退出子对象的独立线程
how to quit an independent thread of a child object by only terminating its parent object
假设我们有一个父类Sensor
。移动机器人有各种传感器(如凸轮、声纳、激光、GPS等)。等等)。每个传感器都有自己的线程,它为机器人提供噪声测量。我想模拟这个场景,因此代码可能如下所示
class Robot
{
update()
{
mSensor[i]->update();
}
Sensor *mSensor;
}
class Sensor
{
update();
}
class GPS : public Sensor
{
Thread mThread();
update();
}
class CAM : public Sensor
{
Thread mThread();
update();
}
在main函数中,基本上我需要创建一个Robot对象,因此
#include "Robot.h"
int main()
{
Robot robo;
while( true ){
robot.update();
}
}
我的问题是如何保证传感器的线程只终止机器人对象?我需要在父析构函数中通过某种循环遍历所有传感器对象吗?
首先不要使用Sensor *mSensor;
,而是使用std::vector<std::unique_ptr<Sensor>>
。其次,每个传感器的析构函数都应该结束线程。然后,当Robot
类被销毁时,向量将被销毁,这将调用析构函数关闭向量中的所有传感器。
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 什么时候调用组成单元对象的析构函数
- "Extern"对象问题:错误:Id 返回 1 个退出状态
- 从std :: fileSystem ::路径对象的类中退出时的分割故障
- 如何将指针分配给功能中的新对象,而不会使该对象在退出后消失
- 全局范围内的对象会导致程序退出时崩溃
- 从具有本地 std::thread 对象而不连接的函数退出
- 即使在函数退出后,也使流对象保持打开状态
- 为什么全局或静态对象会导致程序退出时崩溃
- asio::io_service.run() 尽管工作对象退出
- 退出()是否刷新并关闭“流”对象
- 为什么我的C++程序在使用 NULL 对象时静默退出
- 我是否应该确保在退出程序之前销毁 SDL 2.0 对象(渲染器、窗口、纹理等)
- 如何在 Windows 上的线程退出时释放 TLS 插槽中的对象
- C/C++中的Do对象在程序退出后会被收集到垃圾
- 当拥有的对象退出范围时,在类实例成员上使用shared_ptr是否会正确清理
- 为什么在构造函数退出时创建新的实例(对象)
- 如何通过仅终止子对象的父对象来退出子对象的独立线程
- 退出重载赋值操作符将调用析构函数来删除对象
- Qt 在另一个线程中排队工作线程对象的退出事件