ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
multi_threaded_executor.hpp
1 // Copyright 2014 Open Source Robotics Foundation, Inc.
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 #ifndef RCLCPP__EXECUTORS__MULTI_THREADED_EXECUTOR_HPP_
16 #define RCLCPP__EXECUTORS__MULTI_THREADED_EXECUTOR_HPP_
17 
18 #include <chrono>
19 #include <memory>
20 #include <mutex>
21 #include <set>
22 #include <thread>
23 #include <unordered_map>
24 
25 #include "rclcpp/executor.hpp"
26 #include "rclcpp/macros.hpp"
27 #include "rclcpp/memory_strategies.hpp"
28 #include "rclcpp/visibility_control.hpp"
29 
30 namespace rclcpp
31 {
32 namespace executors
33 {
34 
36 {
37 public:
38  RCLCPP_SMART_PTR_DEFINITIONS(MultiThreadedExecutor)
39 
40 
54  RCLCPP_PUBLIC
55  explicit MultiThreadedExecutor(
57  size_t number_of_threads = 0,
58  bool yield_before_execute = false,
59  std::chrono::nanoseconds timeout = std::chrono::nanoseconds(-1));
60 
61  RCLCPP_PUBLIC
62  virtual ~MultiThreadedExecutor();
63 
68  RCLCPP_PUBLIC
69  void
70  spin() override;
71 
72  RCLCPP_PUBLIC
73  size_t
74  get_number_of_threads();
75 
76 protected:
77  RCLCPP_PUBLIC
78  void
79  run(size_t this_thread_number);
80 
81 private:
82  RCLCPP_DISABLE_COPY(MultiThreadedExecutor)
83 
84  std::mutex wait_mutex_;
85  size_t number_of_threads_;
86  bool yield_before_execute_;
87  std::chrono::nanoseconds next_exec_timeout_;
88 };
89 
90 } // namespace executors
91 } // namespace rclcpp
92 
93 #endif // RCLCPP__EXECUTORS__MULTI_THREADED_EXECUTOR_HPP_
Coordinate the order and timing of available communication tasks.
Definition: executor.hpp:66
RCLCPP_PUBLIC MultiThreadedExecutor(const rclcpp::ExecutorOptions &options=rclcpp::ExecutorOptions(), size_t number_of_threads=0, bool yield_before_execute=false, std::chrono::nanoseconds timeout=std::chrono::nanoseconds(-1))
Constructor for MultiThreadedExecutor.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Options to be passed to the executor constructor.