记录cartographer运行过程中创建了多少个线程。
首先是 main函数里面的运行结果(cartographer_ros::Run();之前)
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7fffe7b9f700 (LWP 35875)]
[New Thread 0x7fffe739e700 (LWP 35876)]
[New Thread 0x7fffe2b9d700 (LWP 35877)]
[New Thread 0x7fffe039c700 (LWP 35878)]
[New Thread 0x7fffddb9b700 (LWP 35879)]
[New Thread 0x7fffdb39a700 (LWP 35880)]
[New Thread 0x7fffd8b99700 (LWP 35881)]
::ros::init
::ros::start
[New Thread 0x7fffd5ac1700 (LWP 35883)]
[New Thread 0x7fffd52c0700 (LWP 35884)]
[New Thread 0x7fffd4abf700 (LWP 35885)]
[New Thread 0x7fffcffff700 (LWP 35886)]
cartographer_ros::ScopedRosLogSink ros_log_sink;
这个表明下面代码块创建了从35875到35881的线程
google::InitGoogleLogging(argv[0]);
google::ParseCommandLineFlags(&argc, &argv, true);
这个表明下面代码块创建了从35883到35886的线程
::ros::start();
然后后端回环检测线程池创建num_background_threads数量的线程
继续往下
对于cartographer_ros::Run();函数其会在tf2_ros::TransformListener tf(tf_buffer);这个函数中创建一个发布监听tf树的线程
继续往下
[New Thread 0x7fffce7fc700 (LWP 35889)]
[ INFO] [1691996207.857706512]: I0814 14:56:47.000000 35870 node.cc:224] node all WallTimer PublishSubmapList
[New Thread 0x7fffcdffb700 (LWP 35890)]
[ INFO] [1691996207.857899289]: I0814 14:56:47.000000 35870 node.cc:230] node all WallTimer PublishLocalTrajectoryData
[ INFO] [1691996207.857931726]: I0814 14:56:47.000000 35870 node.cc:234] node all WallTimer PublishTrajectoryNodeList
[ INFO] [1691996207.857948724]: I0814 14:56:47.000000 35870 node.cc:238] node all WallTimer PublishLandmarkPosesList
[ INFO] [1691996207.857964145]: I0814 14:56:47.000000 35870 node.cc:242] node all WallTimer PublishConstraintList
[ INFO] [1691996207.857979489]: I0814 14:56:47.000000 35870 node.cc:247] node all WallTimer LogLocalization
[ INFO] [1691996207.857996249]: I0814 14:56:47.000000 35870 node.cc:254] node all WallTimer PublishLocalizationQuality
[ INFO] [1691996207.858010825]: I0814 14:56:47.000000 35870 node.cc:259] node all WallTimer PublishPointCloudMap
其中wall_timers_会创建[New Thread 0x7fffce7fc700 (LWP 35889)]线程
publish_local_trajectory_data_timer_会创建[New Thread 0x7fffcdffb700 (LWP 35890)]线程
注意ceres_solver_options中的num_threads,ceres_solver_options会有两个地方使用,一个是前端基于ceres的匹配,另一个是后端优化。
对于前端ceres_solver_options中的num_threads,如果设置为1,那么其不会创建新的线程,会跟主线程一起。
对于后端优化ceres_solver_options中的num_threads,如果设置为7,那么其会额外开启7-1=6个线程
总结,只考虑正常算法运行,即不考虑::ros::start()一堆的线程,其会开启1(tf2_ros::TransformListener) + num_background_threads(回环检测) + 1(wall_timers_) + 1(publish_local_trajectory_data_timer_) + 前端num_threads-1 + 后端num_threads-1 + 1(主线程)。