Sleep() function C++

Jan 5, 2010 at 3:11pm
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.
Jan 5, 2010 at 3:31pm
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.
Jan 5, 2010 at 3:49pm
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.
Jan 5, 2010 at 3:54pm
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 Jan 5, 2010 at 4:00pm
Jan 5, 2010 at 4:15pm
Perfect! I’m not doing any multithreading so I’ll continue to use Sleep. Thanks for the help guys.
Jan 5, 2010 at 4:17pm
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 Jan 5, 2010 at 4:19pm
Jan 5, 2010 at 4:51pm
How do you perform threading in C++ anyway?
Jan 5, 2010 at 5:06pm
You need an external library (eg. boost)
C++0x standard library will allow multi-threading
Jan 5, 2010 at 9:31pm
What does threading do? I understand some of the underlying concepts from java's swing timer class but could someone give me an introduction?
Jan 5, 2010 at 9:36pm
Jan 5, 2010 at 10:16pm
Silly me... always google first.
Topic archived. No new replies allowed.