CPP – [busy] sleep() function via time.h

As you may know, there are multiple ways to create a sleep\delay function in C++.
These may appeal more or less to you depending on system and libraries available.
The following short function uses functionality of time.h header, which makes it useful for cases when dos.h is missing and you do not want to include windows.h just for a single function.

void sleep(int ms) {
	clock_t target = clock() + ms;
	while (clock() < target) { }
}

Related posts:

2 thoughts on “CPP – [busy] sleep() function via time.h

    • It will. That’s the price paid for ease of implementation in this case, which will still be bearable, if application only has to sleep for short periods of time, and use of unistd.h (unix) or windows.h (windows) isn’t preferred.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.