Multithreaded Executor needed for use with another std::thread?

asked 2021-09-21 09:20:08 -0600

flo gravatar image

updated 2021-09-21 11:02:02 -0600

Hey there,

I have a small service server node that also happens to have another std::thread in it (i.e. serial communication). The cb_function of the service is using a member-function through std::bind, as suggested by this answere here.

The first thing I do in the constructor of the node-class is to setup the service by this->create_service(...). When I perform a rclcpp::spin_some(this->get_node_base_interface()) afterwards I see the service via ros2 service list for a very short while (just one spin, so very expected).

I end my constructor by this:

RCLCPP_WARN(this->get_logger(), "starting Thread");
this->thread_inotifier = std::thread([&](){this->; }); //basically a while(1) inside
RCLCPP_WARN(this->get_logger(), "Thread is running -> should spin now");

As the node only has one service, I cannot further veryfy if it really runs. I see parameter-services from my node, but not the actual service.

When I include this at the very end of my constructor, it operates as expected:

while (rclcpp::ok())

The main from which my node is constructed looks like this:

int main(int argc, char ** argv)
  rclcpp::init(argc, argv);
  rclcpp::NodeOptions options;
  auto node = std::make_shared<myPackage::MyClass>(options);

  return 0;

Is there any obviouse I'm doing wrong? I'm thinking about using a MultiThreadedExecutor, but I only thought that I would use it for responding to multiple Action/Service Requests from the same node.

Kind regards :)

Edit: I use galactic on ROS2 inside an ARM docker-container

edit retag flag offensive close merge delete



This is the same exact problem I'm also facing.

pmuthu2s gravatar image pmuthu2s  ( 2021-09-21 10:52:02 -0600 )edit