c++ - 在 C++ 中,首先发生的是返回对象的拷贝还是本地对象的析构函数?

标签 c++ multithreading

我想在某个地方有一个答案,但我找不到它,因为有很多线程问题,相比之下我的问题很简单。

我不是要创建线程安全的拷贝或赋值构造函数或类似的东西。

我想知道的是,如果我有一个代表互斥锁的类,并且我从一个实例化它的函数返回,首先发生的是我的互斥锁的析构函数(从而解锁它)或复制构造函数返回值。这是我的例子:

string blah::get_data(void)
  {
    MutexLock ml(shared_somewhere_else); // so this locks two threads from calling get_data at the same time

    string x = "return data";
    return x;
  }

在其他地方,我们调用 get_data...

 string result = get_data();

回想一下 C 语言,您永远不会返回指向全局变量的指针,因为局部变量在您返回后会超出范围。

C++ 没有这个问题,因为 x 会被复制到结果中。我想知道的是什么时候会发生。在复制之前我的锁会被释放吗?

在这个简单的例子中,“返回数据”是静态信息,但我正在使用的是可以由另一个线程更改的数据(也锁定在同一个互斥锁上)所以如果锁在复制到之前释放-result 已生成,拷贝可能已损坏。

我不确定我是否很好地解释了这个问题,所以我会尝试澄清这是否没有意义。

最佳答案

对于以前的标准(这里我将使用 C++ 03),最接近在返回中声明操作序列的标准来自 6.6

6.6 Jump statements

  1. On exit from a scope (however accomplished), destructors (12.4) are called for all constructed objects with automatic storage duration (3.7.2) (named objects or temporaries) that are declared in that scope, in the reverse order of their declaration. Transfer out of a loop, out of a block, or back past an initialized variable with automatic storage duration involves the destruction of variables with automatic storage duration that are in scope at the point transferred from...

return 语句必须完成才能退出 [function] 范围,这意味着复制初始化也必须完成。此命令不明确。 3.7.212.8 中的各种其他引文简明扼要地说明与上述相同,但没有提供明确的顺序。工作修订(2014 年 11 月之后)包括以下引用以解决该问题。 defect report阐明变化。

From the current working draft (N4527) of the standard as seen on the date of this question

6.6.3 The Return Statement

  1. The copy-initialization of the returned entity is sequenced before the destruction of temporaries at the end of the full-expression established by the operand of the return statement, which, in turn, is sequenced before the destruction of local variables (6.6) of the block enclosing the return statement.

请注意,此引用直接引用 6.6。所以我认为可以安全地假设在返回表达式复制初始化返回值之后 Mutex 对象将始终被销毁。

关于c++ - 在 C++ 中,首先发生的是返回对象的拷贝还是本地对象的析构函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33150508/

相关文章:

java - 用 Java 下载多个文件

c++ - 将 sigwait 与 std::thread 和管道一起使用

c++ - 对原子变量的非原子操作

c++ - 我正在尝试在 C++ 中制作一个介于 5 和 8 之间的随机数生成器

c++ - MPL替换而不类型转换聚变容器

c++ - 如何在 ubuntu 14.04 中通过 apt-get 安装以前版本 (4.4.7) 的 gcc/g++?

java - 并发问题 - 从 Java 多线程服务器下载的同一文件的大小不同

java - 如果在 java 中创建线程并运行,则创建一个数组

c++ - 查找 vector C++ 的唯一元素

c++ - 我们如何使用下面的集合构造函数示例构造集合元素?