Qthread several functions __qthread functions that must be understood

Source: Internet
Author: User
Tags numeric value terminates
Overview

If you want to have a deeper understanding of the Qthread in Qt, you need to know these important functions, and now you're going to introduce them. Introduction to Functions

Property return value function Body function
Static Qthread * Qthread::currentthread () Returns a pointer to the current thread, a static function.
Static Qt::handle Qthread::currentthreadid () Returns the handle to the current thread, static function
Static bool Qthread::isfinished () const Returns true if thread execution ends, otherwise returns false
Static bool Qthread::isrunning () const Returns true if the current thread is running, otherwise returns false
Static Int Qthread::idealthreadcount () Returns the number of threads supported by the system in the ideal state. If no discovery detects the processor's core, the return value is-1
Protected Int EXEC () Causes the thread to enter the event loop state and is in a wait state until the exit () function is called to exit. The return value when exiting is an input parameter when the exit () function is invoked. If you call the Quit () function, its exit value is 0. This function is typically called in the Run () function, which causes the thread to enter the event-loop processing state.
Protected void Exit (int returncode = 0) Tells the thread to exit from the event loop state and returns the value of the ReturnCode. Generally, a return of 0 indicates a successful exit, and a value of not 0 indicates an error was encountered. When the function is called, the thread does not handle the event again unless the EXEC () function is called again. If the current thread is not executing, the next call to Exec () is returned directly
Private signal void Finished () The signal is emitted before the thread is finished, and when the signal is emitted, it means that it has already exited the event-looping state, that is, any events other than the deferred delete event (deferred deletion) are no longer being processed. You can connect the signal to the Qobject::d eletelater () to remove the object from the thread. If you force the use of the Terminate () function to end a thread, you will not know the sending thread of the finish () signal. In addition, this is a private signal, so the user cannot send this signal.
Static bool qthread::isinterruptionrequested () const Returns whether a task running on the current thread can be stopped, and can be interrupted by requestinterruption (). This function can cause a long-running task to stop completely and never check whether the return value of the function is safe. However, a regular use of the method is advocated for long time tasks. However, do not use this method frequently to avoid the overhead of thread switching.
Static bool Qthread::isrunning () const Returns whether the current thread is running, returns true if it is running, or false
Static Int Qthread::looplevel () const This function returns the level of the current event loop, but the function can only be called within a thread.
Static void Qthread::msleep (unsigned long msecs) Force thread Rest msecs milliseconds
Static void Qthread::usleep (unsigned long usecs) Force thread Rest Usecs microseconds
Static void Qthread::msleep (unsigned long secs) Force thread rest secs seconds
Virtual protected void Qthread::run () The function is the starting point for the thread to run. After calling the start () function, the newly created function calls the function, and the default qthread simply invokes exec () into the event loop mechanism. We can overload this function to implement a more advanced approach to thread management. Returning from this function will end the thread.
Static void Qthread::setpriority (Priority Priority) This function sets the priority of the thread to run, and if the thread is not running, the function does nothing. You can specify a priority to start a thread by calling start (). The priority effect depends on how the operating system is scheduled, especially on operating systems that do not support precedence, and priority settings are ignored.
Slot void Qthread::quit () Tells the thread that the event loop exits and returns a value of 0, equivalent to calling Qthread::exit (0). If the thread does not have an event loop, the function does nothing.
Virtual protected void Qthread::setstacksize (UINT stacksize) Sets the maximum value for the thread stack, and if the value set is greater than 0, the stack maximum is set to the current numeric value. Otherwise, the maximum value of the thread stack is determined by the operating system. Warning: Most operating systems set the maximum minimum value for the thread stack themselves, and the thread does not start if the stack size is set beyond range.
Static protected void qthread::setterminationenabled (BOOL enabled = TRUE)
Slot void Qthread::start (Priority Priority = inheritpriority) Start the thread by calling the run () function. The operating system dispatches threads according to priority. If the thread is already running, the function does nothing. Priority settings depend on how the operating system is scheduled to be threaded.
Signal void Qthread::started () The signal is emitted when the thread starts executing, before the run () function is called. Note: This is a private signal, so it can only be emitted by the thread, and the user cannot emit the signal.
Slot void Qthread::terminate () Terminates the current thread. Threads may not be terminated immediately, depending on the scheduling policy of the thread. In general, call the function and then call qthread::wait () to ensure that the thread ends. When the thread terminates, other threads waiting for the thread will be awakened. Warning: This function is more dangerous and is not recommended. The thread may terminate at any code point. It may be terminated when the data is modified, and the thread will not be able to do the cleanup after it finishes.
Static bool Qthread::wait (unsigned long time = Ulong_max) Blocks the current process until one of the following two conditions is met: 1. The associated thread completes its task, and then if the thread has ended, the function returns True, and the function returns True if the thread does not start. 2. After a certain length of time, if the time is Ulong_max (the default), then the wait () function will hardly timeout. (that is, the function must be returned from the run () function) if the wait function times out, the function returns FALSE.
Static void Qthread::yieldcurrentthread () Give the current thread execution power to another executable thread. As for which executable thread to give, that is the operating system.
Use TipsAfter a general call to the Quit () function, you can immediately invoke the wait () function to ensure that the thread exits. Sleep (), and so on, functions that allow threads to hibernate do not need to be invoked because the QT thread is an event-driven mechanism. But if it is an inherited Qthread class that uses an infinite loop in the run () function, consider the Msleep () function to break the thread for a period of time, typically 1 milliseconds.

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.