ROS中Callbacks和Spinning的那些事儿:ros::spin ros::spinOnce ros::MultiThreadedSpinner ros::AsyncSpinner

ros::spin()  单线程回调函数

ros::spin()在节点关闭或ros::shutdown()或按下Ctrl-C才会返回。

ros::spinOnce() 单线程回调函数

ros::spinOnce()在那个时间点,会调用所有的回调函数。

ros::MultiThreadedSpinner 防阻塞多线程回调函数

MultiThreadedSpinner类似于ros::spin(),在构造过程中可以指定它所用线程数,但如果不指定线程数或者线程数设置为0,它将在每个cpu内核开辟一个线程。
用法如下:

ros::MultiThreadedSpinner spinner(4); // Use 4 threads
spinner.spin(); // spin() will not return until the node has been shutdown

ros::AsyncSpinner 防阻塞多线程回调函数

AsyncSpinner比MultiThreadedSpinner更优,它有start() 和stop() 函数,并且在销毁的时候会自动停止。

下面的用法等价于上面的MultiThreadedSpinner例子。

ros::AsyncSpinner spinner(4); // Use 4 threads
spinner.start();
spinner.stop();
ros::waitForShutdown();

建议

A callback should stay as simple and fast as possible.

In most of the cases, a callback is just feeding ROS data to your own classes which is usually as simple as copying data (and maybe converting them). It will help you ensuring that your callbacks are thread-safe (if you want to convert your node to a nodelet for instance, one day) and avoid making "ros::Spin" blocking for a long time, even in the case you are using the single-threaded spinner.


参考

https://wiki.ros.org/roscpp/Overview/Callbacks%20and%20Spinning

https://answers.ros.org/question/53055/ros-callbacks-threads-and-spinning/

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

abcwoabcwo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值