82nd lesson, Thread life cycle

Source: Internet
Author: User

one, the life cycle of the thread

1, a project in the actual

(1),C + + object has a life cycle

(2), thread also has a life cycle

(3), the experience in engineering practice to find ways to ensure: Thread object (Qthread object) life cycle > corresponding thread life cycle

Problem code: After defining the local thread Object T.start () in the stack, the inheritance is executed down, then the thread object is destroyed, and the member variable i is destroyed, but run () is not finished, it will operate a destroyed object, the program crashes

second, the design of synchronous thread

1. Concept: Thread object is actively waiting for thread life to end before destroying

2. Features

(1), while supporting the creation of thread objects in stacks and heaps

(2), the object is destroyed to ensure that the end of the thread life cycle

3. Important: Call the Wait () function in the destructor to force the wait thread to run the end

4, use occasions: the thread life cycle is relatively short case

three, asynchronous threading design

1. Concept: Notification of the destruction of thread objects at the end of the thread life cycle

2. Features

(1), you can only create thread objects in the heap ( declare constructors as private, so define static member functions like second-order constructs to create objects )

(2), thread object can not be actively destroyed by the outside ( declare the destructor as private )

3. Key points:

(1), the last call to the Deletelater () function in Run () (as the name implies Deletelater () can only create objects on the heap)

(2), thread body function active request to destroy Thread object (apply to QT platform)

4, use occasions: the thread cycle is not controllable, it takes a long time to run in the background of the situation

int Main (intchar0x1c08void0x2050void0 void 1 void 2 void Asynthread::run () Endasynthread::~asynthread ()// called destructor, indicating no memory leaks
   #ifndef mythread_h   #define  Mythread_h #include  <qthread>class  MyThread: public   qthread{q_object  public  :  explicit     MyThread (Qobject *parent = 0   public   slots:  private  :  void     Span style= "COLOR: #000000" > run (); };  #endif  //  Mythread_h   
Problematic Thread MyThread.h
#include"MyThread.h"#include<QDebug>Mythread::mythread (Qobject*parent): Qthread (parent) {}voidMythread::run () {qdebug ()<<"void Mythread::run () tid="<<Qthread::currentthreadid ();  for(intI=0; i<3; i++) {qdebug ()<<"void Mythread::run () i="<<i; Sleep (1); } qdebug ()<<"void Mythread::run End";}
Problematic Thread MyThread.cpp
   #ifndef synthread_h   #define  Synthread_h #include  <qthread>class  synthread: public   qthread{Q_object  public  :      Explicit  synthread (qobject *parent = 0   ~synthread ();  private  :      void   run (); };  #endif  //  Synthread_h   
Synchronous Thread SynThread.h
#include"Synthread.h"#include<QDebug>Synthread::synthread (Qobject*parent): Qthread (parent) {}voidSynthread::run () {qdebug ()<<"void Synthread::run () tid="<<Qthread::currentthreadid ();  for(intI=0; i<3; i++) {qdebug ()<<"void Mythread::run () i="<<i; Sleep (1); } qdebug ()<<"void Synthread::run () End";} Synthread::~Synthread () {wait ();//the end of the waiting thread cycle in the destructorQdebug () <<"Synthread::~synthread ()";}
Synchronous Thread SynThread.cpp
#ifndef Asynthread_h#defineAsynthread_h#include<QThread>classAsynthread: Publicqthread{Q_object Public:    Staticasynthread* newinstance (Qobject *parent=0);Private:    voidrun (); ExplicitAsynthread (Qobject *parent =0);//constructors and destructors are defined as private~asynthread ();};#endif //Asynthread_h
Asynchronous Thread AsynThread.h
#include"AsynThread.h"#include<QDebug>Asynthread::asynthread (Qobject*parent): Qthread (parent) {}asynthread* Asynthread::newinstance (Qobject *parent) {    return NewAsynthread (parent);}voidAsynthread::run () {qdebug ()<<"void Asynthread::run () tid="<<Qthread::currentthreadid ();  for(intI=0; i<3; i++) {qdebug ()<<"void Asynthread::run () i="<<i; Sleep (1); } qdebug ()<<"void Asynthread::run () End"; Deletelater ();//be careful to put the last}asynthread::~Asynthread () {qdebug ()<<"Asynthread::~asynthread ()";}
Asynchronous Thread AsynThread.cpp
#include <QtCore/QCoreApplication>#include"MyThread.h"#include"Synthread.h"#include"AsynThread.h"#include<QDebug>#include<QThread>voidMythread () {mythread T; T.start ();}voidSynthread () {Synthread st; St.start ();}voidAsynthread () {Asynthread* at =asynthread::newinstance (); //Asynthread t;//compile error, constructor private at-start (); //Delete at;//error compiling, destructor private}intMainintargcChar*argv[])        {Qcoreapplication A (argc, argv); Qdebug ()<<"int main (int argc, char* argv[]) tid="<<Qthread::currentthreadid (); //mythread ();//program Crashes//Synthread ();//synchronous thread, running correctlyAsynthread (); returna.exec ();}
main.cpp Iv. Summary

(1), the life cycle of the thread object must be greater than the corresponding thread's life cycle

(2), synchronous thread Design---thread life-cycle is short

(3), asynchronous Threading Design---Thread life-time is not controllable

(4), thread class design must adapt to specific occasions

(5), no universal design, only the right design

82nd lesson, Thread life cycle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.