Q Close three functions of a thread

Source: Internet
Author: User

The QT thread has 3 functions that can close the thread, respectively:
1. void Quit () is equivalent to exit (0).

2. void exit (int returncode = 0)
When exit is called, thread exits the event loop and returns from Exec, and the return value of exec is ReturnCode.
Usually returncode=0 indicates success, and other values indicate failure.

3. void Terminate ()
The end thread, whether the thread terminates immediately depends on the operating system.
When the thread is terminated, all threads waiting for the thread to finished will be awakened.
Whether the terminate is called depends on the setterminationenabled (bool enabled = TRUE) switch.

Where quit and terminate are slots, can be directly connected with the signal to close the thread, but generally not recommended to use terminate, and want to close the thread, most likely, the following example, directly call the Stop interface, the thread will be closed.
recommendation : In general, an identifier is set inside the run function to control the loop stop. The Quit function is then called to exit the thread.

Class Thread:public Qthread
{
Q_object
Public
Thread ();

void Setmessage (const QString &message);
void Stop ();

Protected
void run ();

Private
QString Messagestr;
volatile bool stopped;
};

Thread::thread ()
{
stopped = false;
}
void Thread::run ()
{
while (!stopped)
Std::cerr << qprintable (MESSAGESTR);
stopped = false;
Std::cerr << Std::endl;
}
void Thread::stop ()
{
stopped = true;
}

Q Close three functions of a thread

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.