由浅到深理解ROS(8)-线程管理

转自

 

 

单线程Spinning

ros::spin()是最简单的单线程自旋, 它会一直调用直到结束

用法:  ros::spin();

另一个单线程spinning是ros::spinOnce(),它定期调用等待在那个点上的所有回调

用法:  ros::spinOnce();

简单的我们自己实现一个用法相同的ros::spin()

这样:  ros::getGlobalCallbackQueue()->callAvailable(ros::WallDuration(0.1));

ros::spinonce

这样:  ros::getGlobalCallbackQueue()->callAvailable(ros::WallDuration(0));


以上,是它的基础用法,那么spin到底做了什么呢?

首先, 当我们调用ros::spin时, 会有一个互斥锁, 把你的回调队列加锁, 防止执行混乱. 

然后, 检测如果回调队列不为空, 则读取回调队列

最后,当while(nh.ok())为true时, 调用当前队列中的所有函数,如果有不满足的, 会重新放回队列中

 

所以listener中, 就一直执行这ros::spin来监听话题了.从这样看来,spin和spinOnce的区别之一,就是while(nh::ok())执行块的大小了. 另一个是等待时间, spin在执行时, 会指定一个返回前可以等待调用的时间. spin会等待0.1s而spinonce不会

spinOnce使得pub/sub为非阻塞锁 spin是客户端的, 因此是阻塞的.

这样就很好理解talker要用SpinOnce,有需要talk的时候发出,没有的时候不发送.而listener一直在阻塞着听


这样,再来说之前很流传的一句关于解释spin的话, "所有的回调函数都是spin调用的". 这是一句形象而不准确的话. 回调函数一直等待在回调队列中, 只要条件一满足就会发生回调, 而spin的作用, 只是创建了线程给这个回调函数去执行它, 这样多线程就不会影响其他的作业.

之所以用spin, 是因为rospy不愿指定线程模型, 在程序中将线程暴露出来, 而用spin来把它封装起来. 但你可以用多线程调用任意数量的回调函数.

没有用户订阅, 服务和回调是不会被调用的.


 

 

多线程Spinning

多线程Spinning

roscpp内部支持调用多线程, 有两个:

ros::MultiThreadedSpinner

ros::MultiThreadedSpinner是阻塞微调, 类似于ros::spin(), 你可以在它的构造函数中指定线程数量, 但如果不指定或者设为0, 它会根据你的CPU内核数创建线程.

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

ros::AsyncSpinner (since 0.10)

一个更有用的线程spinner是AsyncSpinner. 与阻塞的spin()不同, 它有start()和stop()调用, 并且在销毁时自动停止

1 ros::AsyncSpinner spinner(4); // Use 4 threads
2 spinner.start();
3 ros::waitForShutdown();

 

 

附官方说明:

 

Single-threaded Spinning

 

The simplest (and most common) version of single-threaded spinning is ros::spin(): 

 

切换行号显示

   1 ros::init(argc, argv, "my_node");2 ros::NodeHandle nh;3 ros::Subscriber sub = nh.subscribe(...);4 ...5 ros::spin();

In this application all user callbacks will be called from within the ros::spin() call. ros::spin() will not return until the node has been shutdown, either through a call to ros::shutdown() or a Ctrl-C. 

Another common pattern is to call ros::spinOnce() periodically: 

 

切换行号显示

   1 ros::Rate r(10); // 10 hz2 while (should_continue)3 {4   ... do some work, publish some messages, etc. ...5   ros::spinOnce();6   r.sleep();7 }

ros::spinOnce() will call all the callbacks waiting to be called at that point in time. 

Implementing a spin() of our own is quite simple: 

 

切换行号显示

   1 #include <ros/callback_queue.h>2 ros::NodeHandle n;3 while (ros::ok())4 {5   ros::getGlobalCallbackQueue()->callAvailable(ros::WallDuration(0.1));6 }

and spinOnce() is simply: 

 

切换行号显示

   1 #include <ros/callback_queue.h>2 3 ros::getGlobalCallbackQueue()->callAvailable(ros::WallDuration(0));

Note: spin() and spinOnce() are really meant forsingle-threaded applications(单线程的), and are not optimized for being called from multiple threads at once. See the multi-threaded spinning section for information on spinning from multiple threads. 

 

Multi-threaded Spinning

roscpp provides some built-in support for calling callbacks from multiple threads. There are two built-in options for this: 

ros::MultiThreadedSpinner

  • MultiThreadedSpinner is ablocking spinner(阻塞的), similar to ros::spin(). You can specify a number of threads in its constructor, but if unspecified (or set to 0), it will use a thread for each CPU core. 

    切换行号显示
       1 ros::MultiThreadedSpinner spinner(4); // Use 4 threads2 spinner.spin(); // spin() will not return until the node has been shutdown3 
    

ros::AsyncSpinner (since 0.10)

  • AsyncSpinner API (Jade)

  •  
  • A more useful threaded spinner is the AsyncSpinner(异步的). Instead of a blocking spin() call, it has start() and stop()calls, and will automatically stop when it is destroyed. An equivalent use of AsyncSpinner to the MultiThreadedSpinner example above, is: 

    切换行号显示
       1 ros::AsyncSpinner spinner(4); // Use 4 threads2 spinner.start();3 ros::waitForShutdown();

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440022.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机网络通讯协议

网络通讯&#xff1a; 就是要把特定意义的数据通过物理介质传送给对方。把电信号变成有意义的数据&#xff1a; 以字节为单位分组&#xff0c;标识好每一组电信号的信息特征&#xff0c;按照分组的顺序来依次发送。 以太网规定&#xff1a;一组电信号为一个数据包&#xff0c…

【CodeForces - 518D】Ilya and Escalator(概率dp,数学期望)

题干&#xff1a; Ilya got tired of sports programming, left university and got a job in the subway. He was given the task to determine the escalator load factor. Lets assume that n people stand in the queue for the escalator. At each second one of the tw…

Apollo进阶课程 ② | 开源模块讲解(上)

目录 1&#xff09;无人驾驶车介绍 2&#xff09;高精地图 3&#xff09;定位 4&#xff09;感知 5&#xff09;轨迹规划 6&#xff09;控制 7&#xff09;云端 原文链接&#xff1a;Apollo进阶课程 ② | 开源模块讲解&#xff08;上&#xff09; Apollo自动驾驶进阶课…

由浅到深理解ROS(9)- 几个基本概念的理解 坐标系 包

1.坐标系 最常用的就是map&#xff0c;odom&#xff0c;base_link&#xff0c;base_laser坐标系&#xff0c;这也是开始接触gmapping的一些坐标系。 map:地图坐标系&#xff0c;顾名思义&#xff0c;一般设该坐标系为固定坐标系&#xff08;fixed frame&#xff09;&#xff…

计算机常用技巧

windows禁用自带键盘命令&#xff08;笔记本&#xff09; 禁用&#xff1a;sc config i8042prt start disabled 恢复&#xff1a;sc config i8042prt start auto锁屏 win l

【POJ - 1698】Alice's Chance(网络流最大流,建图)

题干&#xff1a; Alice, a charming girl, have been dreaming of being a movie star for long. Her chances will come now, for several filmmaking companies invite her to play the chief role in their new films. Unfortunately, all these companies will start mak…

由浅到深理解ROS URDF教程

创建自己的URDF文件 1.1创建树形结构文件 在这部分教程中要创建的将是下面的图形所描述的机器人的urdf文件 图片中这个机器人是一个树形结构的。让我们开始非常简单的创建这个树型结构的描述文件&#xff0c;不用担心维度等的问题。创建一个my_robot.urdf文件&#xff0c;内容如…

wireshark基本使用及介绍

Wireshark使用 注&#xff1a;本文中使用的wireshark是3.2.2版本 捕获过滤器表达式 下面是常用的捕获过滤器&#xff0c;wireshark中&#xff1a;捕获->捕获过滤器 除此外&#xff0c;还可以指明传输方向&#xff0c;如&#xff1a;src&#xff08;源方向&#xff09;, …

1.2)深度学习笔记------神经网络的编程基础

目录 1&#xff09;Binary Classification 2&#xff09;Logistic Regression 3&#xff09;Logistic Regression Cost Function 4&#xff09;Gradient Descent 5&#xff09;Logistic Regression Gradient Descent&#xff08;重点&#xff09; 6&#xff09;Gradient …

CS231n Convolutional Neural Networks for Visual Recognition------Python Tutorial

源链接为&#xff1a;http://cs231n.github.io/python-numpy-tutorial/。 这篇指导书是由Justin Johnson编写的。 在这门课程中我们将使用Python语言完成所有变成任务&#xff01;Python本身就是一种很棒的通用编程语言&#xff0c;但是在一些流行的库帮助下&#xff08;numpy&…

【HDU - 3081】Marriage Match II(网络流最大流,二分+网络流)

题干&#xff1a; Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the game of playing house we used to play when we are kids. What a happy time as so many friends playing together. And it is …

IP、TCP、UDP、HTTP头部信息

IP头部信息 ip报文段格式 版本&#xff1a; 占4位&#xff0c;表明IP协议实现的版本号&#xff0c;当前一般为IPv4&#xff0c;即0100。报头长度 &#xff1a; 占4位&#xff0c;因为头部长度不固定&#xff08;Option可选部分不固定&#xff09;&#xff0c;所以需要标识…

ROS技术点滴 —— MoveIt!中的运动学插件

MoveIt!是ROS中一个重要的集成化开发平台&#xff0c;由一系列移动操作的功能包组成&#xff0c;提供运动规划、操作控制、3D感知、运动学等功能模块&#xff0c;是ROS社区中使用度排名前三的功能包&#xff0c;目前已经支持众多机器人硬件平台。 MoveIt!中的众多功能都使用插件…

1)机器学习基石笔记Lecture1:The Learning Problem

网上关于机器学习的课程有很多&#xff0c;其中最著名的是吴恩达老师的课程&#xff0c;最近又发现了NTU林轩田老师的《机器学习基石》课程&#xff0c;这门课也很好。课程总共分为4部分&#xff0c;总共分为16节课&#xff0c;今天来记录第一节课。 When Can Machines Learn?…

MMS协议

MMS格式解析 简介&#xff1a; MMS是微软的私有流媒体协议。 它的最初目的是通过网络传输多媒体广播、视频、音轨、现场直播和一系列的实时或实况材料。 MMS建立在UDP或TCP传输&#xff0f;网络层上&#xff0c;是属于应用层的。使用TCP的MMS上URL是MMS://或者MMST://&#x…

【HDU - 6118】度度熊的交易计划(最小费用可行流,网络流费用流变形 )

题干&#xff1a; 度度熊参与了喵哈哈村的商业大会&#xff0c;但是这次商业大会遇到了一个难题&#xff1a; 喵哈哈村以及周围的村庄可以看做是一共由n个片区&#xff0c;m条公路组成的地区。 由于生产能力的区别&#xff0c;第i个片区能够花费a[i]元生产1个商品&#xff0…

老王说ros的tf库

ros的tf库 为了这个题目&#xff0c;我是拿出了挤沟的精神挤时间&#xff0c;是下了功夫的&#xff0c;线性代数、矩阵论复习了&#xff0c;惯性导航里的dcm、四元数也了解了&#xff0c;刚体力学也翻了&#xff0c;wiki里的欧拉角也读了&#xff0c;tf的tutorial、paper、sou…

Apollo进阶课程 ③ | 开源模块讲解(中)

目录 1&#xff09;ISO-26262概述 2&#xff09;ISO-26262认证流程 3&#xff09;ISO-26262优点与缺陷 原文链接&#xff1a;Apollo进阶课程 ③ | 开源模块讲解&#xff08;中&#xff09; Apollo自动驾驶进阶课程是由百度Apollo联合北京大学共同开设的课程&#xff0c;邀请…

python 问题集

打开文件是报&#xff1a;UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xe9 in position 0: unexpected end of data UnicodeDecodeError:“utf-8”编解码器无法解码位置0中的字节0xe9:unex 在open中加入encodingunicode_escape 如&#xff1a; with open(file_n…

【HDU - 6447】YJJ's Salesman(降维dp,树状数组优化dp)

题干&#xff1a; YJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination. One day, he is going to travel from city A to southeastern city B. Let us assume that A …