【发布时间】:2014-08-22 21:05:36
【问题描述】:
我是 C++11 中 std::thread 的新手。我尝试将functor-object 传递给线程而不是lambda。
然而,我得到的输出让我吃惊:析构函数似乎被调用了 6 次。
代码片段:
#include <iostream>
#include <thread>
struct A
{
A()
{
std::cout << "creating A" << std::endl;
}
void operator()()
{
std::cout << "calling ()-operator" << std::endl;
}
~A()
{
std::cout << "destroying A" << std::endl;
}
};
int main()
{
{
std::thread t( (A()) );
t.join();
}
std::cin.get();
}
我得到的输出 (msvc11) 是
creating A
calling ()-operator
destroying A
destroying A
destroying A
destroying A
destroying A
destroying A
非常感谢任何解释。!
【问题讨论】:
-
我会添加一个复制构造函数来查看它被调用了多少次......
-
您还可以将输出添加到复制和移动构造函数,以查看实现对对象的作用。但是,它是您特定标准库实现的实现细节。
-
赞,复制构造函数被调用了 5 次,因此这 6 个 dtor 调用非常有意义。剩下的问题是:为什么有 5 个副本?
-
您可能对此感兴趣。 stackoverflow.com/a/24191884/893693
-
@user3796535 移出的对象仍需要销毁。移动必须使源对象处于“未指定但有效”状态。 dtor 当然也会在此类对象上调用。
标签: c++ multithreading c++11 destructor