QT blocking delay and non-blocking delay (four methods of personal notes)

Source: Internet
Author: User
Tags mutex sleep cpu usage high cpu usage

/***************************************************************************************** * Function Description: Millisecond non-blocking delay function
* Parameter: Delay millisecond number
* Other instructions: tested, available, but with high CPU usage
*****************************************************************************************/
void Vdpumngui::msecsleep (int msec)
{
Qtime dietime = Qtime::currenttime (). Addmsecs (msec);
while (Qtime::currenttime () <dietime)
Qcoreapplication::p rocessevents (qeventloop::allevents, 100);
}


/*****************************************************************************************
* Function Description: Millisecond-level non-blocking delay function
* Parameter: Delay millisecond number
* Other instructions: not tested
*****************************************************************************************/
void Vdpumngui::msecsleep (int msec)
{
Qmutex Mutex;
Qwaitcondition sleep;
Mutex.lock ();
Sleep.wait (&mutex, 1000);
Mutex.unlock ();
}


/*****************************************************************************************
* Function Description: Millisecond blocking Delay function: API function sleep (int msec)
* Parameter: Delay millisecond number
* Other instructions: tested, available, note the first letter S to capitalize, basically do not occupy the CPU
*****************************************************************************************/


/*****************************************************************************************
* Function Description: Millisecond blocking delay function
* Parameter: Delay millisecond number
* Other instructions: not tested
*****************************************************************************************/
void Vdpumngui::msecsleep (int msec)
{
Qtime N=qtime::currenttime ();
Qtime now;
do{
Now=qtime::currenttime ();
}while (N.msecsto (now) <=msec);
}

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.