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