[QT] qthread class details

Source: Internet
Author: User
Tags usleep

This class provides independent threads for the platform.
# Include <qthread>
Inherit qobject
Public type
Enum
Priority {idlepriority, lowestpriority, lowpriority, normalpriority,
..., Inheritpriority}
Common functions
Qthread (qobject * parent = 0)
~ Qthread () void exit (INT returncode = 0) bool isfinished () constbool
Isrunning
() Constpriority priority () constvoid setpriority (Priority priority
) Void setstacksize (uint
Stacksize) uint stacksize () constbool wait
(Unsigned long time = ulong_max)
Public slot
Void quit () void start (
Priority priority = inheritpriority) void terminate () Signal
Void
Finished () void started () void terminated () Static Public Member
Qthread *
Currentthread () Qt: handle currentthreadid () int idealthreadcount () void
Yieldcurrentthread ()
Protected Functions
Int exec () virtual void run
() Static protected members
Void msleep (unsigned long msecs) void
Setterminationenabled (bool enabled = true)
Void sleep (unsigned
Long SECs) void usleep (unsigned long usecs) Detailed Description
A qthread represents an independent thread.
Control in the program. It shares data and all other threads, but runs independently in a multitasking operating system. Replace start
In Main, qthread starts at run. Default
, Run start time loop, by calling exec. To create your own thread, create a subclass of qthread and inherit run.
For example:
Class
Mythread: Public qthread
{
Public:
Void run ();
};
Void mythread: Run ()
{
Qtcpsocket socket;
//
Connect qtcpsocket's signals somewhere meaningful
...

Socket. connecttohost (hostname, portnumber );
Exec ();
}
This will create
Create a qtcpsocket in this thread and run the time loop of the thread. Use the start method to start running. When you return from run, the running ends, just like a response
When
You leave main. Qthread will note that you send a signal when the thread started, finished, terminated, or you can use isfinished
And isrunning
To query the thread status and use wait to block it until the thread has completed running.
Each thread gets its own stack from the operating system. The operating system is also
Determines the default size of the stack. You can use setstacksize to set the stack size of a convention.
Each qthread can have its own event loop. You can start the event Loop
Previously called exec. You can stop it by calling exit or quit. Has an event loop to enable signal and
The connection of the slot becomes possible. A mechanism is called queue link. It also enables
Classes that require event loops become possible, such as qtimer and qtcpsocket. Note:
In this case, it is impossible to use any widget class.
In extreme situations
You may want to force terminate a thread. Nevertheless, doing so is dangerous and should be blocked. Read the file terminate and
Setterminationenabled.
Jing
The State functions currentthreadid and currentthread return the identifier of the currently running thread.
Qthread also provides platform-independent sleep Functions
Number. Use sleep, msleep, and usleep.
Member type file
Enum qthread: Priority
Enum type
How does the operating system arrange new threads.
Content value description
Qthread: idlepriority 0
Schedule only when no other thread is running
Qthread: lowestpriority 1 arrangement is less than the low priority
Qthread: lowpriority 2
Schedule less than normal priority
Qthread: normalpriority 3 default Operating System priority
Qthread: highpriority 4
Higher than normal priority
Qthread: highestpriority 5 arrangement is higher than high priority
Qthread: timecriticalpriority
6. Arrange as much as possible
Qthread: inheritpriority 7 uses the same priority, which is the default
Member function File
Qthread: qthread (qobject *
Parent = 0)
Construct a new thread to use the given parent. This thread does not start running until start is called.
Qthread ::~ Qthread
()
Destroy thread
Note that deleting a thread object will not stop the running thread. Deleting a running qthread may cause a program conflict. You can wait to make sure it is complete
.
Qthread * qthread: currentthread () [Static]
Returns
Needle.
Qt: handle qthread: currentthreadid () [Static]
Return the thread handle.
Alert
The handle returned by the function is used for internal purposes and should not be used for any application code.
In Windows, the returned value is a false handle. It cannot be compared as a numerical value. This
The function returns DWORD and getcurrentthreadid of the Win32 function.
Handle returned by getcurrentthread.
Int
Qthread: exec () [protected]
Enter the event loop and wait until exit is called. If exit is called through quit, the return value is
It is 0. It is necessary to call this function to start the event loop.
Void qthread: exit (INT returncode = 0)
Notice
The event loop of the litigation thread to exit
After calling this function, the thread leaves the event loop and returns the result from qeventloop: exec (). And return code.
According to the agreement,
If the return value is 0, the operation is successful. If the return value is not 0, the operation fails.
Unlike the C function, this function returns caller-it is event processing thar.
Stops.
If there is no event loop, this function does not do anything.
Void qthread: finished () [Signal]
This
Signal is sent when the thread ends running.
Int qthread: idealthreadcount () [Static]
The returned result can be run in
The ideal number of threads on the system.
Bool qthread: isfinished () const
Returns true if the thread is finished.
Bool
Qthread: isrunning () const
Void qthread: msleep (unsigned long
Msecs) [Static protected]
Priority qthread: Priority () const
Return
Returns the priority of a thread. If the function does not run, the return value is inheritpriority.
Void qthread: Quit () [Slot]
And so on
Equivalent to exit (0 ).
Void qthread: Run () [Virtual protected]
The start point of the thread. Before calling
After start, the new thread calls this function, and the default inheritance calls exec.
You can reload this function to do other useful work.
Void
Qthread: setpriority (Priority priority)
Set priority
Void
Qthread: setstacksize (uint stacksize)
Set the maximum stack size to a thread.
Warning: most operating systems are pre-configured
Set the maximum and minimum limits on the thread stack size. This thread will start to fail, if the stack is out of this limit
Void
Qthread: setterminationenabled (bool enabled = true) [static
Protected]
Set the validity of the terminate () function.
Void qthread: Sleep (unsigned
Long SECs) [Static protected]
Uint qthread: stacksize () const
Void
Qthread: Start (Priority priority = inheritpriority) [Slot]
Start running
The program calls run.
Void qthread: started () [Signal]
This signal is sent when the thread starts.
Void
Qthread: Terminate () [Slot]
The thread that stops running. This thread may be terminated immediately or delayed. In terminate
And then use wait to terminate synchronization.
Warning: this function is dangerous and is not encouraged.
Void qthread: terminated ()
[Signal]
Void qthread: usleep (unsigned long usecs) [static
Protected]
Bool qthread: Wait (unsigned long time = ulong_max)
Resistance
Until these conditions are met
The thread stops running.
Time has passed. (Timeout)
Void
Qthread: yieldcurrentthread () [Static]
Open a thread to another runable thread. Note that this is determined by the operating system.

 

 

The QT thread has a public abstract class. All threads are derived from this qthread abstract class. To implement the pure virtual function run () in qthread, the run () function is
The START () function. For example:

1 class mythread: Public qthread {
2 public:
3 Virtual void run ();
4 };
5
6 void mythread: Run ()
7 {
8 For (int count =
0; count <20; count ++ ){
9 sleep (1 );
10
Qdebug ("Ping! ");
11}
12}
13
14 int
Main ()
15 {
16 mythread;
17 mythread B;
18

19 A. Start (); // run () is automatically called; otherwise, even if the thread is created, it is suspended at the beginning.
20
B. Start ();
21 // wait for thread a and thread B to exit
22 A. Wait ();
23
B. Wait ();
24}
25

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.