Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: How to sleep for a nanosecond? [duplicate]

Tags:

c++

sleep

windows

Possible Duplicate:
Sleep Less Than One Millisecond

How can I make a program sleep for a nanosecond? I searched the Internet, and I found several ways to sleep, but:
windows.h's Sleep() sleeps only for milliseconds.
ctime's nanosleep() is only for POSIX systems, and I'm using Windows.
I also tried this:

int usleep(long usec)
{
    struct timeval tv;
    tv.tv_sec = usec/1000000L;
    tv.tv_usec = usec%1000000L;
    return select(0, 0, 0, 0, &tv);
};

But Code::Blocks says:

obj\Release\main.o:main.cpp|| undefined reference to `select@20'|

I tried many things, but everything failed. What should I do?

like image 362
SmRndGuy Avatar asked Jun 16 '12 13:06

SmRndGuy


People also ask

What is nanosleep in C++?

nanosleep is a POSIX compliant system call for suspending the program execution for the given amount of fixed time period. Other functions also provide the facilities to do the same operation, sleep is one of them, which takes a number of seconds to suspend the calling process. sleep is said to provide the low-resolution suspension.

What is the value of the nanoseconds field in nanosleep?

The value of the nanoseconds field must be in the range 0 to 999999999. Compared to sleep (3) and usleep (3), nanosleep () has the advantage of not affecting any signals, it is standardized by POSIX, it provides higher timing resolution, and it allows to continue a sleep that has been interrupted by a signal more easily.

How do I sleep in milliseconds in C program?

C Program to Sleep in Milliseconds We don’t have millisecond level control in the sleep()function. But in Linux we can implement our own function that will take duration in milliseconds as input and sleep for that duration. Using usleep() The usleep()function takes the duration in micro seconds as input.

What is sleep () function and how to use it in C?

What Is sleep () function and How To Use It In C Program? C programming language provides sleep () function in order to wait for a current thread for a specified time. slepp () function will sleep given thread specified time for the current executable. Of course, the CPU and other processes will run without a problem.


2 Answers

Using C++11

#include <chrono>
#include <thread>
...
std::this_thread::sleep_for(std::chrono::nanoseconds(1));

Note that the implementation may sleep longer than the given period.

like image 119
Stephan Dollberg Avatar answered Oct 19 '22 23:10

Stephan Dollberg


You should also notice that there is the scheduler, which probably allows no sleeps that are shorter than an timeslice (somewhat around 4 ms - 10 ms, depending on your windows and machine). sleeping less than that is not possible on

Here are some (quite old) research on that issue windows.

This article suggests using Win32 timeBeginPeriod() to achieve that.

like image 35
Mare Infinitus Avatar answered Oct 20 '22 00:10

Mare Infinitus