Multithreaded Executor needed for use with another std::thread?
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->notifier_.run(); }); //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())
{
rclcpp::spin_some(this->get_node_base_interface());
}
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);
rclcpp::spin(node->get_node_base_interface());
rclcpp::shutdown();
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
Asked by flo on 2021-09-21 09:20:08 UTC
Comments
This is the same exact problem I'm also facing.
Asked by pmuthu2s on 2021-09-21 10:52:02 UTC