Using timers - periodic functioning of a code on Jetson TX2

Hi ,

How to call a function within a C++ code at specific intervals (5ms,10ms) periodically without the usage of sleep/delay on Jetson TX2?
Are there any inbuilt software timers that can be used for the above purpose?

Kindly help,

Thanks,
Pratosha

This isn’t really an answer, but there are so many ways to time things. More information would help, e.g., the answer will be quite different between timers in a kernel driver versus in user space. Timers which use a single clock to trigger multiple threads may also differ versus just a single process/thread. You should give much more detail on what will know about the timer, what the timer will do when triggered, so on.

As LinuxDev said, there isn’t really an answer, I am assuming you want to send something like a CAN message every {X}ms or something like that… There may be an issue with how long it takes your message to leave the user space and go into the kernel space, but I would be surprised if this was the bottleneck…

I would suggest as a starting point, to look at pthread as there are so many resources and examples available.

https://computing.llnl.gov/tutorials/pthreads/
https://randu.org/tutorials/threads/
https://vcansimplify.wordpress.com/2013/03/08/pthread-tutorial-simplified/
http://timmurphy.org/2010/05/04/pthreads-in-c-a-minimal-working-example/

Hi,
Thanks for your views david and linuxdev.
@David - going through the links shared. Thankyou.
We are just trying to modify jetson-inference code so that Pedestrian detection happens every 30ms and CAN signals corresponding to (brake/accelerate/horn) are sent every 10ms.
We don’t want to use sleep function as this can hault the process for that duration of time.

We want the code to be like -

while(1)
{
if (timer ==10ms)
{

//perform CAN functions.

}

if (timer ==30ms)
{

//perform detectnet functions

}
}

I hope this has given a clarity on our requirement.

Thanks,
Pratosha

Linux is “soft real time” so you don’t get microsecond precision in scheduling, although you do have microsecond resolution.
A pthread with realtime priority and SCHED_RR scheduling policy will be your best bet.

Additionally, you’ll want to use setitimer() or timer_create(). You may also want to use nanosleep() or select() as an additional high-precision blocking primitive.

https://linux.die.net/man/2/setitimer
https://linux.die.net/man/2/timer_create

BEWARE, though, that signals in UNIX are somewhat like interrupt handlers in microcontrollers. You don’t know what else a program was doing while a signal was being delivered. It may, for example, be holding a heap allocation lock, so calling any library function that in turn might attempt to allocate memory, or block in some other way, from a signal handler, is a reciepe for hard-to-track-down rare bugs. Typically, you’ll just want to set a flag, and have your main loop pick up the event. Signals DO generally make various sleep and wait functions return with EINTR/-1, which makes it possible to check the flag and do the thing quickly.

@Prtoshaa

Snarky makes some great points there, “A pthread with realtime priority and SCHED_RR

I think you’re talking about millisecond resolution, not microsecond, so I think you’re safe. My concern is how long your detecting software takes to run. Also, I know you’ve just posted pseudocode, but try structure your software such that you can manage your timers and threads efficiently, don’t do much processing within the one thread, etc.

Hi,

Yes will surely look into it.

Thankyou.

Hi,
Sorry for the delay in my response .
We have 2 timers - one for running CAN signals and the other for detectnet.
We want the code to run on a single GPU /single CPU without interfering with other cores.
Is that possible?
Kindly help.

Thanks,
Pratosha

Hi,

You can try cudaSetDevice() to specify the GPU device you want to use.
For Jetson, only one GPU is available.

Thanks.