- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在 C++ RAII 中,当对象超出范围时会调用 dtor。多线程机制总是涉及传递回调以在新线程上运行。因此,我认为一个对象不可能在一个线程中构造并在另一个线程中被破坏,因为它们必须是不同的范围。
我说得对吗?或者在某些情况下可以在不同线程中调用对象 ctor 和 dtor?
最佳答案
Thus, I don't think it's possible for an object to be constructed in one thread and destructed in another, since those would have to be different scopes.
Am I right about this?
没有。这些函数可以完全独立于任何线程调用。
Or are there some cases where an objects ctor and dtor can be called in different threads?
当然有这种情况(只是日常面包和黄油)。考虑一个简单的生产者/消费者模型和通过队列在线程之间交换的消息实例。
生产者线程创建消息实例并将其插入队列。消费者将其从队列中取出,处理完成后将调用消息析构函数。
关于c++ - 对象的 ctor 和 dtor 必须在同一个线程上吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44976448/
当类具有普通构造函数和/或普通析构函数时,C++ 标准定义了一些非常具体的行为。 例如,根据标准的 §3.8/1: The lifetime of an object of type T ends w
我正在学习一些关于格式化字符串错误利用的教程,在某些时候他们谈到覆盖 dtors 表。但这是我在使用 nm 时发现的: 080495a8 d _DYNAMIC 0804969c d _GLOBAL_O
我正在学习一些关于格式化字符串错误利用的教程,在某些时候他们谈到覆盖 dtors 表。但这是我在使用 nm 时发现的: 080495a8 d _DYNAMIC 0804969c d _GLOBAL_O
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题?通过 editing this post 添加详细信息并澄清问题. 1年前关闭。 Improve this
在 C++ 中>=11,是否可以安全地从析构函数中抛出异常,即仅在没有异常处于事件状态时才抛出异常? 我试过: #include #include #include struct foo{
编译器给出链接错误,要求提供 Base 纯虚拟析构函数的定义。 class Base { public: virtual ~Base() = 0; }; class Derived : pub
我想确保析构函数的副作用保留在作为 RVO 候选者的函数中。我的目标是在进入和退出时对堆栈进行快照,并显示预期的堆栈变量。这段代码似乎适用于 C++11 而无需使用特定于编译器的选项,但我不知道如何在
我在 gcc 4.4.5 上尝试了以下代码。 如果成员“data”不存在,代码执行正常,但在它存在的情况下,它会崩溃。当派生类的 dtor 不是虚拟时,它也不会崩溃。 我知道在这两种情况下行为都是未定
我的代码中有以下类。换句话说,CTor中有一个创建线程的静态对象(单例),当它的DTor被调用时,它有一些工作要做在这个线程的上下文中(DTor为线程放置一些工作)。 我面临的问题是,当调用 B 的
我正在阅读《黑客,剥削的艺术》一书。书中有一节解释了.dtors和.ctors的使用。 我正在尝试重现书中的一个练习,但在我的可执行文件中我没有这些部分。起初我以为问题是我正在为 64 位编译,但现在
这个问题在这里已经有了答案: Move-ctor and copy-ctor not called (3 个答案) C++11 move constructor not called, defaul
正如我在Move constructor/operator=中询问的那样,过了一段时间,我同意并接受了这个问题的正确答案,我只是在想,是否有类似“移动析构函数” 这样的东西会在每次移动的对象上调用会有
Herb Sutter 在他的文章中 http://www.gotw.ca/gotw/047.htm指出我们不能在析构函数中使用uncaught_exception, // Why the wron
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭10
我正在尝试从接口(interface)向下转型到派生类,但我的虚拟 dtor 杀死了它? class IFOO { public: virtual ~IFOO(){}; virtual size_
我想通过如下初始化语句实现数组对象的初始化。 TestClass array[5] = { TestClass("test1"), TestClass("test2"), Te
在我正在调试的代码库中遇到了这个析构函数。 ManagerImpl::~ManagerImpl() { // don't go away if some thread is still hit
这个问题在这里已经有了答案: When to use virtual destructors? (20 个答案) 关闭 8 年前。 我正在尝试找出 C++ 中类继承的技巧,并且我构建了一个示例项目:
在下面的例子中,类 test1 没有 ctor/dotr 而 test2 有空的 ctor 和dtor. 在分配大量test1 和test2 时,空的ctor 和dtor 会降低性能吗? #inclu
我有一个非常复杂的软件,我随机崩溃(主要是在 Release模式下,我无法轻松构建 SSCCE),并在 boost::signal1 的析构函数中跟踪崩溃. 该信号对象由多个对象使用(订阅/取消订阅)
我是一名优秀的程序员,十分优秀!