Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
node_thread.cpp
1 // Copyright (c) 2019 Intel Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <memory>
16 #include "nav2_util/node_thread.hpp"
17 
18 namespace nav2_util
19 {
20 
22  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base)
23 : node_(node_base)
24 {
25  executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
26  thread_ = std::make_unique<std::thread>(
27  [&]()
28  {
29  executor_->add_node(node_);
30  executor_->spin();
31  executor_->remove_node(node_);
32  });
33 }
34 
35 NodeThread::NodeThread(rclcpp::executors::SingleThreadedExecutor::SharedPtr executor)
36 : executor_(executor)
37 {
38  thread_ = std::make_unique<std::thread>(
39  [&]() {
40  executor_->spin();
41  });
42 }
43 
45 {
46  executor_->cancel();
47  thread_->join();
48 }
49 
50 } // namespace nav2_util
~NodeThread()
A destructor.
Definition: node_thread.cpp:44
NodeThread(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base)
A background thread to process node callbacks constructor.
Definition: node_thread.cpp:21