How to do a 5 MicroSeconds delay in application?

Check:

http://qdn.qnx.com/support/docs/neutrino_qrp/lib_ref/n/nanospin.html


“nto” <4871941@mail.sc.cninfo.net> wrote in message
news:9ipbve$8qm$1@inn.qnx.com

5 microseconds is an awfully long time…wouldn’t usleep() be more
appropriate than nanospin? At least then the calling process is
suspended rather than spinning…

cheers,

Kris

Mario Charest <mcharest@zinformatic.com> wrote:

Check:

http://qdn.qnx.com/support/docs/neutrino_qrp/lib_ref/n/nanospin.html



“nto” <> 4871941@mail.sc.cninfo.net> > wrote in message
news:9ipbve$8qm$> 1@inn.qnx.com> …
\


Kris Warkentin
kewarken@qnx.com
(613)591-0836 x9368
“You’re bound to be unhappy if you optimize everything” - Donald Knuth

“Kris Eric Warkentin” <kewarken@qnx.com> wrote in message
news:9iuqbl$sut$1@nntp.qnx.com

5 microseconds is an awfully long time…

wouldn’t usleep() be more
appropriate than nanospin? At least then the calling process is
suspended rather than spinning…

We are talking microseconds here, not milliseconds. A process cannot
be suspended for that small time amount of time, the granularity is
1ms on fast machine and 10ms on slower one.

If you call usleep(1), to get 1 us, on a PC you will most probably
end up being blocked at least 1ms, 1000 times more.

cheers,

Kris

Mario Charest <> mcharest@zinformatic.com> > wrote:

Check:

http://qdn.qnx.com/support/docs/neutrino_qrp/lib_ref/n/nanospin.html


“nto” <> 4871941@mail.sc.cninfo.net> > wrote in message
news:9ipbve$8qm$> 1@inn.qnx.com> …





\

Kris Warkentin
kewarken@qnx.com
(613)591-0836 x9368
“You’re bound to be unhappy if you optimize everything” - Donald Knuth

Would that depend upon whether or not there was actually a context switch
or not? If you called usleep(5) and no other process was running, would
it then work? I was thinking that if you were writing code which does
something every 5 usecs, you’d be sleeping a lot. If usleep() worked then
perhaps your CPU would not be using as much power as nanospin()? Forgive
my ignorance since I write tools applications and don’t know a huge amount
about the low level stuff.

Kris

Mario Charest <mcharest@zinformatic.com> wrote:

“Kris Eric Warkentin” <> kewarken@qnx.com> > wrote in message
news:9iuqbl$sut$> 1@nntp.qnx.com> …
5 microseconds is an awfully long time…

wouldn’t usleep() be more
appropriate than nanospin? At least then the calling process is
suspended rather than spinning…

We are talking microseconds here, not milliseconds. A process cannot
be suspended for that small time amount of time, the granularity is
1ms on fast machine and 10ms on slower one.

If you call usleep(1), to get 1 us, on a PC you will most probably
end up being blocked at least 1ms, 1000 times more.


cheers,

Kris

Mario Charest <> mcharest@zinformatic.com> > wrote:

Check:

http://qdn.qnx.com/support/docs/neutrino_qrp/lib_ref/n/nanospin.html


“nto” <> 4871941@mail.sc.cninfo.net> > wrote in message
news:9ipbve$8qm$> 1@inn.qnx.com> …





\

Kris Warkentin
kewarken@qnx.com
(613)591-0836 x9368
“You’re bound to be unhappy if you optimize everything” - Donald Knuth


Kris Warkentin
kewarken@qnx.com
(613)591-0836 x9368
“You’re bound to be unhappy if you optimize everything” - Donald Knuth

“Kris Eric Warkentin” <kewarken@qnx.com> wrote in message
news:9j1emv$jtc$1@nntp.qnx.com

Would that depend upon whether or not there was actually a context switch
or not? If you called usleep(5) and no other process was running, would
it then work?

No you would still endup with 1ms. What will wake usleep is when the OS
does
its timer manager think which is every 1ms.

I was thinking that if you were writing code which does
something every 5 usecs, you’d be sleeping a lot.

As a matter of fact if you do something like;

while (1) {
usleep(5);
get_things_done();
}

get_things done would be called every 2ms!!! Which is 400 times
slower then using nanosleep(). If the application is driving some
hardware that means throughput 400 time less :wink:


If usleep() worked then
perhaps your CPU would not be using as much power as nanospin()?

Yes that’s true, in fact usleep does work :wink: It’s just not precise
enough (maybe on other platform it could be).

  • Mario

Kris Eric Warkentin <kewarken@qnx.com> wrote:

Would that depend upon whether or not there was actually a context switch
or not? If you called usleep(5) and no other process was running, would
it then work? I was thinking that if you were writing code which does
something every 5 usecs, you’d be sleeping a lot. If usleep() worked then
perhaps your CPU would not be using as much power as nanospin()? Forgive
my ignorance since I write tools applications and don’t know a huge amount
about the low level stuff.

Nope, wouldn’t work. The ticksize (1ms) is the smallest possible
delay that the OS can offer. Sure, the API to usleep() suggests you
can ask for smaller – but the OS can’t give it to you.

The main choices for really tight delays like that are the nanospin()
functions or using some outside source to generate an interrupt after
that amount of time. Usually, the nano* functions are chosen.

-David

QNX Training Services
dagibbs@qnx.com