Multithreading 为什么是C++;线程多次调用析构函数? 我很好奇,为什么一个简单的C++程序,像下面这样的一个程序,在处理STD::线程时,多次调用Work1对象的析构函数。为什么它多次调用析构函数 class work1 { public: work1(int x_) : x(x_) { ++count; std::cout<<"\n[WORKER1] Ctor : "<<count<<std::endl; } ~work1() { std::cout<<"\n[WORKER1] Dtor : "<<count<<std::endl; } void operator() () { for(int i =0; i < x; ++i) { std::cout<<"[WORKER1] Printing this :: "<<i<<std::endl; } } private: int x; static int count; }; int main() { int local_main=5; std::thread t((work1(local_main))); t.join(); std::cout<<"\n[MAIN] From main thread..."<<std::endl; return 0; }
Multithreading 为什么是C++;线程多次调用析构函数? 我很好奇,为什么一个简单的C++程序,像下面这样的一个程序,在处理STD::线程时,多次调用Work1对象的析构函数。为什么它多次调用析构函数 class work1 { public: work1(int x_) : x(x_) { ++count; std::cout<<"\n[WORKER1] Ctor : "<<count<<std::endl; } ~work1() { std::cout<<"\n[WORKER1] Dtor : "<<count<<std::endl; } void operator() () { for(int i =0; i < x; ++i) { std::cout<<"[WORKER1] Printing this :: "<<i<<std::endl; } } private: int x; static int count; }; int main() { int local_main=5; std::thread t((work1(local_main))); t.join(); std::cout<<"\n[MAIN] From main thread..."<<std::endl; return 0; },multithreading,c++11,Multithreading,C++11,std::thread通过调用复制构造函数来复制参数。实施副本构造函数以观察副本: class work1 { public: work1(int x_) : x(x_) { ++count; std::cout << __PRETTY_FUNCTION__ << ' ' << count << '\n'; } work1(work1 const& other) :
std::thread
通过调用复制构造函数来复制参数。实施副本构造函数以观察副本:
class work1 {
public:
work1(int x_) : x(x_) {
++count;
std::cout << __PRETTY_FUNCTION__ << ' ' << count << '\n';
}
work1(work1 const& other)
: x(other.x)
{
++count;
std::cout << __PRETTY_FUNCTION__ << ' ' << count << '\n';
}
work1& operator=(work1 const& other)
{
x = other.x;
std::cout << __PRETTY_FUNCTION__ << ' ' << count << '\n';
return *this;
}
~work1() {
--count;
std::cout << __PRETTY_FUNCTION__ << ' ' << count << '\n';
}
void operator() () {
for(int i =0; i < x; ++i) {
std::cout<<"[WORKER1] Printing this :: "<<i<<std::endl;
}
}
private:
int x;
static int count;
};
int work1::count;
int main() {
int local_main=5;
std::thread t((work1(local_main)));
t.join();
}
如果要避免复制,请使用std::ref
将引用传递给函数对象:
int main() {
work1 w{5};
std::thread t(std::ref(w));
t.join();
}
产出:
work1::work1(int) 1
work1::work1(const work1&) 2
work1::work1(const work1&) 3
work1::~work1() 2
work1::~work1() 1
[WORKER1] Printing this :: 0
[WORKER1] Printing this :: 1
[WORKER1] Printing this :: 2
[WORKER1] Printing this :: 3
[WORKER1] Printing this :: 4
work1::~work1() 0
work1::work1(int) 1
[WORKER1] Printing this :: 0
[WORKER1] Printing this :: 1
[WORKER1] Printing this :: 2
[WORKER1] Printing this :: 3
[WORKER1] Printing this :: 4
work1::~work1() 0
由于我们没有解决方案,我们无法真正告诉您发生了什么,也无法帮助您找出原因以及如何解决任何问题。另外,请花一些时间刷新,以及。它现在更清晰,这只是一个复制(或移动)的情况。线程需要调用函数的副本,在您的情况下,每个副本都是对象的副本(这导致复制或移动构造)然后是可能的临时对象的破坏。我们如何控制并纠正这个问题。但是你不认为抛出同一对象的引用不是一个好主意,因为其他线程也会持有对同一对象的引用。这不是真的吗?很抱歉,我需要澄清这几个概念。@AnubhavRohatgi线程拥有自己的副本是最安全的。对于引用,当另一个线程使用该对象时,该对象必须保持活动状态。
work1::work1(int) 1
[WORKER1] Printing this :: 0
[WORKER1] Printing this :: 1
[WORKER1] Printing this :: 2
[WORKER1] Printing this :: 3
[WORKER1] Printing this :: 4
work1::~work1() 0