Is there an alternative for sleep() in C?

后端 未结 16 1951
无人及你
无人及你 2020-12-02 14:55

In traditional embedded programming, we will give a delay function like so:

for(i=0;i<255;i++)
   for(j=0;j<255;j++);

In the micropro

16条回答
  •  庸人自扰
    2020-12-02 15:35

    Alternatives depend in what you are trying to do and what OS you are on.

    If you just want to waste time, then these might help:

    On most unix-type systems you'll find a 'usleep' function, which is more or less like sleep with greater resolution. Be careful with that one because it usually can not sleep for just one microsecond.

    On some unix-type systems, the select system call can be used with all file descriptor sets zero in order to get a fairly accurate sub-second wait.

    On windows systems, you have Sleep, which is pretty much the same, but taking a number of milliseconds.

    In a multi-tasking operating system, a sleep function can sometimes be given 0 as a parameter. This generally causes the function to give up it's timeslice, but be re-scheduled immediately if no other task is ready to run.

提交回复
热议问题