简体   繁体   English

如何安全地终止线程? (使用指针) c++

[英]How to terminate a thread safely? (with the usage of pointer) c++

I am currently learning multithreading in c++11 and I am confused with the way to terminate a thread safely.我目前正在 c++11 中学习多线程,我对安全终止线程的方式感到困惑。

In c++, I know the way to create threads and use thread.join() to safely ensure main() to wait for all threads to finish before quitting itself.在 c++ 中,我知道创建线程的方法并使用 thread.join() 来安全地确保 main() 等待所有线程完成后再退出。

However, I found that some multithread codes implemented via pointers are able to run even without using thread.join().但是,我发现一些通过指针实现的多线程代码即使不使用 thread.join() 也能够运行。

class Greating
{
public:

    Greating(const int& _i):i_(_i){}
    ~Greating(){}
    int i_;
    void say()
    {
        std::cout << "Hello World" << i_ << std::endl;
    }

};

int main(){
    Greating greating1(1);
    Greating greating2(2);

    std::thread t1(&Greating::say, greating1);
    std::thread t2(&Greating::say, greating2);
    return 0;
}

The code shown above will absolutely report the error "terminate called without an active exception Aborted (core dumped)", because I did not use t1.join() and t2.join().上面显示的代码绝对会报错“terminate called without an active exception Aborted (core dumped)”,因为我没有使用 t1.join() 和 t2.join()。

However, I found in some codes when they use the pointer to manage the thread, this does not become a problem, as shown below.但是,我在一些代码中发现当他们使用指针来管理线程时,这并没有成为问题,如下图所示。

class Greating
{
public:

    Greating(const int& _i):i_(_i){}
    ~Greating(){}
    int i_;
    void say()
    {
        std::cout << "Hello World" << i_ << std::endl;
    }

};

int main(){
    Greating greating1(1);
    Greating greating2(2);

    std::thread* tt1 = new std::thread(&Greating::say, greating1);
    std::thread* tt2 = new std::thread(&Greating::say, greating2);
    return 0;
}

The output is: output 是:

Hello WorldHello World12
Hello World12

There is no error reported.没有报告错误。 This made me very confused.这让我非常困惑。

So my question is:所以我的问题是:

  1. Why when we use pointer to manage the thread, we could not use the function thread.join()?为什么当我们使用指针管理线程时,我们不能使用 function thread.join()?
  2. How to correctly terminate a thread?如何正确终止线程? (probably wait for the callable function to finish?) (可能等待可调用的 function 完成?)

Thanks very much!非常感谢!

When creating objects with dynamic allocation, you have to deallocate the memory with operator delete so it calls appropriate destructor.使用动态分配创建对象时,您必须使用operator delete释放 memory 以便它调用适当的析构函数。

In the first example, two std::thread objects are created.在第一个示例中,创建了两个std::thread对象。 At the end of main function, the destructor std::thread::~thread is called.main function 的末尾,调用了析构函数std::thread::~thread Since the threads are not joined, the destructor reports an error.由于线程没有加入,析构函数会报错。

On the other hand, in the second example, you called operator new so you create objects with dynamic allocation.另一方面,在第二个示例中,您调用了operator new ,以便创建具有动态分配的对象。 But, you didn't call operator delete , so the destructor is not called.但是,您没有调用operator delete ,因此不会调用析构函数。 That is, the program didn't check whether the threads are joined.也就是说,程序没有检查线程是否被加入。

Therefore, the only way to correctly terminate a thread is to call std::thread::join .因此,正确终止线程的唯一方法是调用std::thread::join If you want to use pointers, you have to do as following:如果要使用指针,则必须执行以下操作:

std::thread *th = new std::thread(foo);
...
th->join();
delete th;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM