Delay versus timers

Hello.

I have a little doubt. In many programs, I need to use a delay of quite a large time, for example 100 ms, 500 ms, 1 second that doesnt need to be very exact.

I find very easy to use the delay instruction and to forget the use of timers and message passing (only for this case, of course).

What are the difference between using both options??

Probably using timers the time must be more exact but what about cpu usage and efficiency ( I have read something about that delay use nanosleep and it needs more CPU…). Can you explain it to me a little bit more?

What is the option you would recommend for this case?? (timers and message passing, timers with signals, delays, …etc).

Thank you very much in advance.

There is no problem using delay() other than the fact that your thread is suspended while you wait. Timers are used to allow you to continue processing other work while you wait for whatever it is your waiting for.

You might be confusing nanosleep with nanospin. nanospin is definately CPU intensive, but it should only be used for small amounts of time (< 10usec).

Using the delay call does NOT consume any CPU time. The kernel will wake up the thread after the delay. With timers, you are more flexible - you can for example send a pulse when the timer fires, and have a thread in your process receive it and react on it, while another thread runs.