Equivalent to Sleep() on Linux?

Started by
3 comments, last by vaceX 20 years, 11 months ago
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?
Advertisement
sleep() does reduce cpu usage. For increments smaller than a second, try usleep().

How appropriate. You fight like a cow.
There''s also nanosleep, which doesn''t affect any signals.
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.
Interests: my money-pit car, computer hardware/programming, anything 3D'93 RX-7
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]

This topic is closed to new replies.

Advertisement