rosin-project / rxros

Reactive programming for ROS
BSD 3-Clause "New" or "Revised" License
48 stars 6 forks source link

RxROS operators should use subscribe_on rather than observe_on #31

Open henrik7264 opened 4 years ago

henrik7264 commented 4 years ago

The following operators: publish_to_topic and send_transform should use subscribe_on rather than observe_on:

    template<typename T>
    auto publish_to_topic(const std::string &topic, const uint32_t queue_size = 10) {
        return [=](auto&& source) {
            ros::Publisher publisher(rxros::node::get_handle().advertise<T>(topic, queue_size));
            source.**observe_on**(rxcpp::synchronize_new_thread()).subscribe(
                [=](const T& msg) {publisher.publish(msg);});
            return source;};}

The observe_on will cause the subscriber to execute in a new thread. However, the source will execute in the thread publish_to_topic was called on. If this is the main thread the publish_to_topic will block the main thread. The subscribe_on will ensure that both the source and the subscriber will execute in a new thread and thus not cause the publish_to_topic to block.