C + + Sleep (0)

Source: Internet
Author: User
Tags sleep function

The meaning of Sleep (0) is to discard the current thread execution time slices and put itself in the waiting queue. At this point the other threads will get the time slice to program the program. Sleep (0) can reduce the execution speed of the current thread, such as: Now there are 100 threads in the system (assuming a thread bar) to perform different tasks, and they perform the same priority, and each time the slice is allocated the same length. Now that there is a sleep (0) in the current thread, the wait time for the current thread to get the time slice is extended by 1 time times, which means that it is equivalent to 200 time slices before the time slice is executed.

The function is not in the standard library

But in some compiled systems, there are some system libraries, depending on your environment.
Such as:

Linux, unsigned int sleep (unsigned intseconds), incoming suspend time, successfully returned 0, unsuccessful returns the remaining number of seconds.
The Windows system has the Sleep function (note uppercase), void Sleep (DWORD dwmilliseconds), and provides the number of milliseconds to suspend.

Sleep is to hang yourself up and end the current time slice

For example:

#include <iostream>
#include <windows.h>
using namespace Std;
int main ()
{
Sleep (3000);//pause for 3 seconds s to capitalize
return 0;
}

Use std::this_thread::sleep_for :

:::: (111605 //or Whatever:::: ();

There is also the complimentary std::this_thread::sleep_until .

Prior to C++11, C + + had no thread concept and no sleep capability, so your solution was necessarily platform dependent. Here's a snippet that defines a sleep function for Windows or Unix:

#ifdef#include<windows.h>void(Unsigned){SleepMilliseconds}#else#include<unistd.h>void(Unsigned){ (*1000} #endif       
But a much simpler pre-C++11 method is to use boost::this_thread::sleep

C + + Sleep (0)

Related Article

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.