|
| 1 | +#include <chrono> |
| 2 | +#include <random> |
| 3 | +#include <thread> |
| 4 | + |
| 5 | +#include <rclcpp/rclcpp.hpp> |
| 6 | +#include <std_msgs/msg/string.hpp> |
| 7 | + |
| 8 | +using namespace std::chrono_literals; |
| 9 | + |
| 10 | +class MultiThreadMutuallyExclusiveSubscriber : public rclcpp::Node { |
| 11 | + |
| 12 | +public: |
| 13 | + MultiThreadMutuallyExclusiveSubscriber(rclcpp::NodeOptions node_options) |
| 14 | + : Node("subscriber_node", |
| 15 | + node_options.allow_undeclared_parameters(true)) { |
| 16 | + rclcpp::SubscriptionOptions options; |
| 17 | + options.callback_group = |
| 18 | + create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive); |
| 19 | + |
| 20 | + short_subscriber_ = create_subscription<std_msgs::msg::String>( |
| 21 | + "/short_topic", rclcpp::QoS(10), |
| 22 | + std::bind(&MultiThreadMutuallyExclusiveSubscriber::ShortTopicCallback, |
| 23 | + this, std::placeholders::_1), |
| 24 | + options); |
| 25 | + |
| 26 | + options.callback_group = |
| 27 | + create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive); |
| 28 | + long_subscriber_ = create_subscription<std_msgs::msg::String>( |
| 29 | + "/long_topic", rclcpp::QoS(10), |
| 30 | + std::bind(&MultiThreadMutuallyExclusiveSubscriber::LongTopicCallback, |
| 31 | + this, std::placeholders::_1), |
| 32 | + options); |
| 33 | + |
| 34 | + timer_ = create_wall_timer( |
| 35 | + 1s, std::bind(&MultiThreadMutuallyExclusiveSubscriber::TimerCallback, |
| 36 | + this)); |
| 37 | + |
| 38 | + auto param_change_callback = |
| 39 | + [this](std::vector<rclcpp::Parameter> parameters) { |
| 40 | + for (auto parameter : parameters) { |
| 41 | + if (parameter.get_name() == "use_mutex") { |
| 42 | + use_mutex = parameter.as_bool(); |
| 43 | + if (use_mutex) |
| 44 | + RCLCPP_INFO(get_logger(), "will use mutex"); |
| 45 | + else |
| 46 | + RCLCPP_INFO(get_logger(), "will not mutex"); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + auto result = rcl_interfaces::msg::SetParametersResult(); |
| 51 | + result.successful = true; |
| 52 | + return result; |
| 53 | + }; |
| 54 | + |
| 55 | + callback_handler = |
| 56 | + this->add_on_set_parameters_callback(param_change_callback); |
| 57 | + } |
| 58 | + |
| 59 | +private: |
| 60 | + bool use_mutex{false}; |
| 61 | + struct { |
| 62 | + std::mutex short_topic_mutex; |
| 63 | + std::mutex long_topic_mutex; |
| 64 | + } mutex_list; |
| 65 | + std::string processed_short_string_; |
| 66 | + std::string processed_long_string_; |
| 67 | + void ProcessString(const std::string &raw_string, std::string &output); |
| 68 | + |
| 69 | + rclcpp::Subscription<std_msgs::msg::String>::SharedPtr short_subscriber_; |
| 70 | + rclcpp::Subscription<std_msgs::msg::String>::SharedPtr long_subscriber_; |
| 71 | + rclcpp::TimerBase::SharedPtr timer_; |
| 72 | + OnSetParametersCallbackHandle::SharedPtr callback_handler; |
| 73 | + |
| 74 | + void ShortTopicCallback(const std_msgs::msg::String::SharedPtr msg); |
| 75 | + void LongTopicCallback(const std_msgs::msg::String::SharedPtr msg); |
| 76 | + void TimerCallback(); |
| 77 | +}; |
| 78 | + |
| 79 | +void MultiThreadMutuallyExclusiveSubscriber::ProcessString( |
| 80 | + const std::string &raw_string, std::string &output) { |
| 81 | + output = "** "; |
| 82 | + |
| 83 | + for (char c : raw_string) { |
| 84 | + output.push_back(c); |
| 85 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 86 | + } |
| 87 | + output += " **"; |
| 88 | +}; |
| 89 | + |
| 90 | +void MultiThreadMutuallyExclusiveSubscriber::ShortTopicCallback( |
| 91 | + const std_msgs::msg::String::SharedPtr msg) { |
| 92 | + std::shared_ptr<std::unique_lock<std::mutex>> lock; |
| 93 | + if (use_mutex) |
| 94 | + mutex_list.short_topic_mutex.lock(); |
| 95 | + ProcessString(msg->data, processed_short_string_); |
| 96 | + RCLCPP_INFO(get_logger(), "Setting processed: %s", |
| 97 | + processed_short_string_.c_str()); |
| 98 | + if (use_mutex) |
| 99 | + mutex_list.short_topic_mutex.unlock(); |
| 100 | +} |
| 101 | + |
| 102 | +void MultiThreadMutuallyExclusiveSubscriber::LongTopicCallback( |
| 103 | + const std_msgs::msg::String::SharedPtr msg) { |
| 104 | + |
| 105 | + if (use_mutex) |
| 106 | + mutex_list.long_topic_mutex.lock(); |
| 107 | + ProcessString(msg->data, processed_long_string_); |
| 108 | + RCLCPP_INFO(get_logger(), "Setting processed: %s", |
| 109 | + processed_long_string_.c_str()); |
| 110 | + if (use_mutex) |
| 111 | + mutex_list.long_topic_mutex.unlock(); |
| 112 | +} |
| 113 | + |
| 114 | +void MultiThreadMutuallyExclusiveSubscriber::TimerCallback() { |
| 115 | + if (use_mutex) { |
| 116 | + mutex_list.short_topic_mutex.lock(); |
| 117 | + mutex_list.long_topic_mutex.lock(); |
| 118 | + } |
| 119 | + RCLCPP_INFO(get_logger(), |
| 120 | + "Getting processed strings: \n [long] %s \n [short] %s", |
| 121 | + processed_long_string_.c_str(), processed_short_string_.c_str()); |
| 122 | + |
| 123 | + if (use_mutex) { |
| 124 | + mutex_list.short_topic_mutex.unlock(); |
| 125 | + mutex_list.long_topic_mutex.unlock(); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +int main(int argc, char **argv) { |
| 130 | + rclcpp::init(argc, argv); |
| 131 | + rclcpp::NodeOptions option; |
| 132 | + auto node = std::make_shared<MultiThreadMutuallyExclusiveSubscriber>(option); |
| 133 | + rclcpp::executors::MultiThreadedExecutor executor; |
| 134 | + executor.add_node(node); |
| 135 | + executor.spin(); |
| 136 | + rclcpp::shutdown(); |
| 137 | + return 0; |
| 138 | +} |
0 commit comments