简体   繁体   English

主要用无限循环终止线程

[英]main terminating a thread with infinite loop

I'm quite new with multi-threading. 我对多线程很陌生。 Below is an example code I was trying to run. 以下是我尝试运行的示例代码。 Please forgive syntax errors (if any), as I'm going by memory. 请原谅语法错误(如果有的话),因为我正在记忆中。
The issue with the code below is that it terminates immediately, whereas it should be running forever. 以下代码的问题是,它立即终止,而它应该永远运行。 I wanted to know why the main is calling the deconstructor, even though there is a while (true) statement. 我想知道为什么即使有while (true)语句,主函数仍在调用解构函数。

There are no other classes, so it is not due to interactions with others. 没有其他类别,因此它不是由于与其他人的互动而引起的。 The only thing the main does is to create an object of type myclass, and call function1() below. 主要要做的就是创建一个myclass类型的对象,并在下面调用function1()。

    int main()
    {
        myclass Object;
        Object.function1();

        return 0;
    }

    /**** new file   ******/
    typedef boost::shared_ptr<boost::thread> thread_pointer;

    class myclass
    {
    public:
        // some stuff
        void function1();
    private:
        // some stuff
        void myfunction();
        thread_pointer tr_ptr;  
    };

    ~myclass() {
        tr_ptr->join();
    }

    void myclass::function1()
    {
        // some stuff   
        tr_ptr = thread_ptr(new boost::thread(&myclass::myfunction, this));  

    }

    void myclass::myfunction()
    {
        // some stuff
        while(true){
             // some stuff
        } 
    }

A 2nd thread will get created at the c.function1() call, but then the main thread keeps going. 在c.function1()调用中将创建第二个线程,但随后主线程继续运行。 However, you do have thread_ptr->join() in the desctuctor which will cause an infinite loop (depending on system timing) if you declare and use myclass like in example 1 because the destructor is called as main exits. 但是,在声明器中确实有thread_ptr-> join(),如果您像示例1中那样声明和使用myclass,则会导致无限循环(取决于系统时序),因为析构函数被称为主出口。

The main thread will block on thread->join() until the child thread exits. 主线程将在thread-> join()上阻塞,直到子线程退出。 So for example 1, when the main thread of execution leaves the scope where c is defined, the destructor will be invoked and the main thread will call join() where it will wait until the child thread exits and since the child never exits, the main thread will block forever. 例如,例如,当执行的主线程离开定义c的作用域时,将调用析构函数,并且主线程将调用join(),它将在其中等待子线程退出,并且由于子线程永不退出,因此主线程将永远阻塞。

Example 1: 范例1:

int main(int argc,char *argv[])
{
   myclass c;

   c.function1();
}

However if you declare and use it like this: 但是,如果您这样声明和使用它:

Example 2: 范例2:

int main(int argc,char *argv[])
{
   myclass *c = new myclass();

   c->function1();
}

Then since objects that are created via new must be explicitly destructed then the destructor will not be called and the program will exit immediately because join() is not called. 然后,由于必须显式销毁通过new创建的对象,因此将不会调用析构函数,并且由于未调用join(),程序将立即退出。

if you further change it to this: 如果您进一步将其更改为:

Example 3: 范例3:

int main(int argc,char *argv[])
{
   myclass *c = new myclass();

   c->function1();
   delete c;
}

You get an infinite loop again because again invoking the destructor will cause the main thread to call join() 您再次遇到无限循环,因为再次调用析构函数将导致主线程调用join()

Your main needs to be something like this: 您的主要需求是这样的:

int main()
{
    myclass Object;
    Object.function1();

    while(true){
    }
    // Or wait for a join by the thread...

    return 0;
}

You object's destructor is called because when main terminates, the Object becomes out of scope. 之所以调用对象的析构函数,是因为当main终止时,该Object将超出范围。

Life of Threads are tied to it's calling Process 线程的寿命与其调用过程相关

When a process exits/terminates, so do all of it's threads. 当进程退出/终止时,它的所有线程也都这样做。 A quick way to ensure you process does not terminate is to add a while(true) loop after you create the thread. 一种确保进程不会终止的快速方法是在创建线程后添加while(true)循环。

As mentioned in your comment, you want to "encapsulate" the while loop in the class. 如您的注释中所述,您希望将“ while”循环“封装”在类中。 You may add it in function1() like this: 您可以像这样在function1()添加它:

void myclass::function1()
{
    // some stuff   
    tr_ptr = thread_ptr(new boost::thread(&myclass::myfunction, this));  

    while(true){
        // Do something
    } 

}

However, it will "block" your main thread, losing most advantages of implementing a thread in the first place. 但是,它将“阻塞”您的主线程,首先失去了实现线程的大多数优势。

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

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