Shared data between threads, but executed separately;
QT Threading Qthread is platform-independent;
Usually the main thread executes from main, while other threads are created in the main thread, and other threads are born in Qthread;
1. Thread Priority
A total of 8 priority levels: Thread priority is getting higher and lower from top to bottom.
Constant |
Value |
Description |
Qthread::idlepriority |
0 |
Scheduled only if no other threads is running. |
Qthread::lowestpriority |
1 |
Scheduled less often than lowpriority. |
Qthread::lowpriority |
2 |
Scheduled less often than normalpriority. |
Qthread::normalpriority |
3 |
The default priority of the operating system. |
Qthread::highpriority |
4 |
Scheduled more often than normalpriority. |
Qthread::highestpriority |
5 |
Scheduled more often than highpriority. |
Qthread::timecriticalpriority |
6 |
Scheduled as often as possible. |
Qthread::inheritpriority |
7 |
Use the same priority as the creating thread. This is the default. |
2. Thread Management
2.1. Thread Start
void |
Start (priority priority = inheritpriority) Initiates the execution of the thread and emits a started signal when it is started. |
2.2. Thread execution
Int |
EXEC () Enter thread EventLoop. |
virtual void |
Run () Thread entry. |
2.3. Thread exit
void |
quit () Equivalent to exit (0). |
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. |
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. |
2.4. Thread wait
void |
Msleep (unsigned long msecs ) |
void |
Sleep (unsigned long secs ) |
void |
Usleep (unsigned long usecs ) |
bool |
Wait (unsigned long time = Ulong_max) The thread will be blocked, waiting for time milliseconds. Unlike sleep, wait returns if the thread exits.
|
2.4. Thread Status
bool |
isfinished () const Whether the thread has exited. |
bool |
isrunning () const Whether the thread is still in the running state. |
2.5. Thread Properties
priority |
< Strong>priority () const |
void /td> |
setpriority (priority priority ) |
void |
< Strong>setstacksize (uint stackSize ) |
uint |
stackSize () const |
void |
< strong>setterminationenabled (bool enabled = true) &NBSP; |
3. Thread instances
When we create a thread, the object of that thread class is created first when a new thread is defined from the Qthread derived class, and then when the thread is used.
For example:
{
Protected
void run (); /* Reload Run */
};
void Mythread::run ()
{
Qtcpsocket socket;
Socket.connecttohost (HostName, portnumber); /* Establish a TCP connection */
exec (); /* Enter the event loop */
}
int main ()
{
MyThread thread; /* Use the newly created thread */
Thread.Start (); /* thread executes run (), establishes a TCP connection and enters the event loop until the thread terminates the Exit event loop */
Thread.wait (); /* Wait for thread to exit */
return 0;
}
When deriving a class from qthread, you need to re-implement the virtual function run of Qthread.
void Qthread::run ()[virtual protected]
The function is the thread's entry, and when we start the thread with start (), the new thread executes run (). The default run () function simply calls exec () into the event loop.
Of course, when you define your own thread run (), you can also not use the event loop,
For example:
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)/* The thread does not use EXEC () to enter the event loop */
Std::cerr << qprintable (MESSAGESTR);
stopped = false;
Std::cerr << Std::endl;
}
void Thread::stop ()
{
stopped = true;
}
QT Thread (i): Thread class