Do Timers run in different threads? [roscpp]
I'm wondering if I'm setting up different timers, do they run in different threads using roscpp? The background is that one Timer callback is taking a longer time to be executed and, therefore, I want to run it at a lower frequency. But it should not interrupt the execution frequency of ros::spin()
(because of the callbacks there) and the other Timer callback.
So my open questions are:
- Do different Timer callbacks run in different threads?
- Do callback functions of the same Timer run in different threads or what is happening if the frequency of a Timer is too high and execution of callback takes longer than this?
- Do Timers run in a different thread than
ros::spin()
, or isros::spin()
setting the minimal time? From what I've read, this is the case and the Timer is executed duringros::spin()
. If callbacks duringros::spin()
take longer than the Timer duration, what will happen? - And as a possible solution: If I switch to AsyncSpinner, does this also affect the Timer callbacks?
What I'm doing now is described in this solution: https://answers.ros.org/question/5305...
Typically, if you want to do time-consuming computations such as "leg detection", the callbacks are definitively not the place to do it.
Just, copy your data to a LegDetector object instance and call in your main thread the method that will do the heavy work.
However, how should this not block my other callbacks? I'm calling the computational intense function during while(ros::ok())
and it is blocking the ROS spinner and, therefore, every other callback. So what is the intention behind this solution?
Sources: http://wiki.ros.org/roscpp/Overview/T... https://roboticsbackend.com/roscpp-ti... :
Once created, the timer will return so the following of the code will be executed. It will then, in another thread, call the callback every X seconds, where X is the first parameter you gave to the Timer.
https://answers.ros.org/question/2403... :
No, topicCallback and timerCallback cannot be executed in parallel, as long as you are using single-threaded spinning. For example, if you are using an AsyncSpinner, callbacks are called from multiple threads and additional care needs to be taken.