Sleep() function C++

Can anyone tell me what functions are better to use in C++ other than Sleep? I’ve heard of Pause and milSleep but I’m not sure if there is a better way to go.

Any help would be greatly appreciated.


Brad.
What's wrong with Sleep()?

I don't think there's anything in the standard lib, so whatever function you use depends on what lib you're using. Other than that, they're pretty much all the same.
Sounds good, I read around some other forums and other people were saying that “Sleep isn’t safe to use”. I’m not sure why but I thought I’d ask and get some more opinions on it.
They didn't give a reason why it's not safe?

I guess it depends on what you're doing. AFAIK, Sleep does exactly what it claims to do.

EDIT:

After googling it looks like the "unsafe" stuff spawns from multithreading issues. I haven't really read them in detail, but I don't see why it would be bad in those situations. I'll have to read more about it after work.

What kind of situation do you need Sleep() for? Will this be in a multithreading environment?

EDIT2:

This link seems to be something:

http://blogs.msdn.com/oldnewthing/archive/2005/10/04/476847.aspx

Basically: dont' use Sleep to yeild CPU time to other threads. That's not what it's for.

But if you're using it to sleep, then it's fine.
Last edited on
Perfect! I’m not doing any multithreading so I’ll continue to use Sleep. Thanks for the help guys.
Basically: dont' use Sleep to yeild CPU time to other threads. That's not what it's for.
That's not what it says. For starters, the very purpose of Sleep() is to yield CPU time.
The link talks about a rather specific situation. It talks about trying to yield CPU from a highest priority thread using Sleep(0). It doesn't apply if the thread is not the one with the highest priority or if you sleep with Sleep(x>0).
Last edited on
How do you perform threading in C++ anyway?
You need an external library (eg. boost)
C++0x standard library will allow multi-threading
What does threading do? I understand some of the underlying concepts from java's swing timer class but could someone give me an introduction?
Silly me... always google first.
Topic archived. No new replies allowed.