I want to use Sleep() but don't want to #include <windows.h>

Pages: 12
How can I implement a Sleep() function codewise?
No. Sleeping is a platform-specific thing which tells the scheduler to give CPU time to other processes. You cannot simulate this without additional libs.
This is so NOT beginners topic. Sleep() yields whatever time slice is left for the thread and then the OS will re-schedule it but only after the specified time has ellapsed. This means the OS needs to be informed of the chosen time.

So how do you do this? By calling Sleep() from Windows.h!! hehe You just cannot emulate it.
edit:
I just read here http://www.cplusplus.com/forum/general/17955/ that it is unsafe to use Sleep() to yield cpu time to other threads, which is exactly why I want to use sleep.

My programs tend to hog cpu usage, by as much as 50% for a hello world in a while(1) loop or something like that.

So what else can I do?
You could use the deprecated CRT function _sleep() -- it's declared in stdlib.h This assumes you are linking to msvcrt.dll.

It will complain, that you should use the new Sleep() function, but it's still there (it behaves just like Sleep as it's a call through to Sleep)

But you wouldn't use this function for commercial or open source code.
I just read here http://www.cplusplus.com/forum/general/17955/ that it is unsafe to use Sleep() to yield cpu time to other threads, which is exactly why I want to use sleep.


That articles says it's unsafe to use Sleep(0). It says nothing about Sleep(1) (which works decently enough).
If you just want to sleep for a tiny bit, you should use SwitchToThread(). It will just do the first part of Sleep(), which is most likely what you need.
where is switchtothreat declared and what if the 'first' part of sleep? and whats the second?
I'm not sure if this works, but I've been told that if you use a FOR loop with the iterations being 1000 for example, it will effectively sleep for 1000 ms. Once again, this is not confirmed.
What you're suggesting, packetpirate, will hold up the thread of execution but it won't return time back to the processor. In fact, it'll take up a *lot* of compute cycles that way.

I see no problem using Sleep(), usleep(), or a platform-independent (read: macros) equivalent.

-Albatross
packetpirate - 1000 iterations of what? An empty loop?

I'm curious to know where you heard this information!?

Computers double in speed every 18 months (Moore's law), so the time 1000 loops is getting smaller and smaller...
or a platform-independent (read: macros) equivalent.


macros?
It's perfectly safe to use Sleep() to yield as long as your code doesn't assume that between the time Sleep(x) is called and the time it returns, exactly x milliseconds have passed. In reality, the only guarantee you have is that the thread will yield for at least x milliseconds.

webJose: Don't be sarcastic when it's your fault for not making yourself clear.
@helios: Don't tell me what I can or cannot do.
closed account (1vRz3TCk)
@helios: Don't tell me what I can or cannot do.

but now your telling helios what to do, when will the madness end? :0)

Disclaimer: this post is meant in jest, please do not shout at the poster as he has a note from his doctor about avoiding undue stress.
My programs tend to hog cpu usage, by as much as 50% for a hello world in a while(1) loop or something like that.
Your program needs a lot of processor time (infinite). The planner, in all its benevolence, tries to follow "Ask, and Ye Shall Receive" (actually, he hates you, and wants you to die as soon as possible).
If you don't want to monopolise the resources, "be nice" and lower your priority (and by lower I meant raise it).
However, be aware that your program now will take longer to finish (it is... more infinite)

xP
Could someone please translate what ne555 just said for me? I don't speak jabberwockey.
ne555 is saying (I think!): Lower your worker thread's priority using SetThreadPriority() (http://msdn.microsoft.com/en-us/library/ms686277(VS.85).aspx ).

Personally I stay away from messing up thread priorities as much as possible so as to not imbalance the complex scheduling algorithm of the operating system. But the function is out there so you might as well try it out.
Generally speaking, it's a much worse problem when a program is not using 100% of a CPU. It means it's wasting a lot of time waiting for some I/O device to catch up and it's running much slower than it otherwise could.
Pages: 12