Archived

This topic is now archived and is closed to further replies.

vaceX

Equivalent to Sleep() on Linux?

Recommended Posts

Right now, I''m using this to delay: counter = GetTicks(); while((counter+50) > (GetTicks())) {} It works, but it takes up 99% of the CPU-time. So I realized there might be something for linux that is like Sleep() on windows (which doesn''t make the CPU work like crazy). sleep() exists, but I''m not sure it reduces the CPU-usage, and it doesn''t take milliseconds either. Anyone know something that might help me?

Share this post


Link to post
Share on other sites
sleep() does reduce cpu usage. For increments smaller than a second, try usleep().


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
sleep() does reduce cpu usage.

Only for the current thread/process. If there are other active execution targets (i.e. other active threads or processes), then they will run while the former target sleeps. Depending on how busy your system is, your sleeping target may sleep fractionally longer than you intended. sleep() and the like are often used for target switching, but they can also help your program be more "polite" (this seems like your intention). Just be careful that your program''s politeness doesn''t interfere with it''s stability.

Share this post


Link to post
Share on other sites
The best trick is to employ a mixture of sleep() and busy-wait loops... sleep for the majority of the time you need, and busy-wait the rest. You would generally free up enough CPU time for the other apps, and the busy-wait will be more accurate when you need to execute the next frame.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

Share this post


Link to post
Share on other sites