The sleep function of C + + in Linux and Windows

Source: Internet
Author: User
Tags function prototype sleep function usleep

Description: function name: Sleep

Function: Execution hangs for a period of time

Usage: unsigned sleep (unsigned seconds);

Using the top file in VC

#include <windows.h>

In the GCC compiler, the header file used differs depending on the GCC version

The header files that the Linux system needs to add #include <unistd.h>

Note

:

In VC, the first English character in sleep is an uppercase "S"

In standard C is sleep, do not capitalize. The following uses uppercase to illustrate, specifically what to see you with what compiler. Simply say VC with sleep, all other uses sleep.

The general form of the sleep function:

Sleep (unisgned long);

The units in sleep () are in milliseconds, so if you want the function to be stuck for 1 seconds, it should be sleep (1000);

Under Linux, the "s" in Sleep is not very written

The units in sleep () are seconds, not milliseconds.

Linux, with g++ compiled, you have to add unistd.h header file, GCC will add no more, because standard C has a sleep file.

Example

#include <windows.h>

int main ()

{

int A;

a=1000;

Sleep (a);/* VC use sleep*/

return 0;

}

Application:
#include <syswait.h>
Usleep (n)//n μs
Sleep (n)//n ms
Sleep (N)//n sec
Drivers:
#include <linux/delay.h>
Mdelay (n)//milliseconds its implementation
#ifdef NOTDEF
#define MDELAY (n) (\
{unsigned long msec= (n), while (msec--) udelay (1000);})
#else
#define MDELAY (n) (\
(__builtin_constant_p (n) && (n) <=max_udelay_ms)? Udelay ((n) *1000): \
({unsigned long msec= (n), while (msec--) udelay (1000);}))
#endif
Calling Asm/delay.h's udelay,udelay should be nanosecond-delay.

Dos:
Sleep (1); Stay for 1 seconds
Delay (100); Stay 100 milliseconds
Windows:
Sleep (100); Stay 100 milliseconds
Linux:
Sleep (1); Stay for 1 seconds
Usleep (1000); Stay 1 milliseconds
Each platform is not the same, it is best to define a set of cross-platform macro control

Seconds or microseconds? About the delay function sleep () because the sleep () function is required to write a piece of code, in my Mind, sleep (10) seems to be dormant for 10 microseconds, and the result is 10 seconds of hibernation (under Linux). It's strange, because the chief remembers it as if it were microseconds.     So I looked it up a bit. The original Sleep function prototype under Linux is: unsigned int sleep (unsigned int seconds), while the Sleep function prototype in MFC is: void Sleep (DWORD Dwmilliseco NDS); That is, the sleep () function is in seconds, sleep (1), or dormant for 1 seconds, under Linux (using the GCC library). While the sleep () function under MFC is in microseconds, sleep (1000) is dormant for 1 seconds. Oh, yes.     If you sleep with a subtle unit under Linux, you can use the thread sleep function: void Usleep (unsigned long usec); Of course, don't forget the # include <system.h> oh when you use it. Also worth mentioning is that there is a delay () function under Linux, the prototype is extern void delay (unsigned int msec), it can delay msec*4 milliseconds, that is, if you want to delay a second, you can use delay (250);

http://blog.csdn.net/jiangxinyu/article/details/7754664

The sleep function of C + + in Linux and Windows

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.