admin管理员组文章数量:1353700
I am creating two different threads which start running when an object gets created.
Is it ok to join these threads inside the destructor then as below?. Hence, wait for the threads to complete before fully destructing the object.
If the threads never terminate, then the object will never fully destruct when it is going out of scope and the main function will never return as in the case below.
Is this a problem, and is there a better recommended way to do this? Thanks.
MyClass::MyClass()
{
// t1, t2 are private member variables
t1 = thread(&MyClass::producer, this);
t2 = thread(&MyClass::consumer, this);
}
MyClass::~MyClass()
{
//Join threads
if (t1.joinable())
{
t1.join();
}
if (t2.joinable())
{
t2.join();
}
}
int main()
{
MyClass mc;
}
本文标签: multithreadingJoining std threads in the destructor in CStack Overflow
版权声明:本文标题:multithreading - Joining std threads in the destructor in C++ - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743936804a2564832.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论