How to stop SingleThreadedExecutor::spin() ?
ROS2 crystal, Ubuntu 18 x64.
Hello, I faced a problem with stopping a thread with SingleThreadedExecutor::spin() inside. The last string is never printed... the only way to solve - uncomment sleep(1)
1) Why spin() is blocked if no actual work ?(no nodes added to SingleThreadedExecutor)
2) How to stop it correctly? Probably cancel() is executed before the spin() call.
class test_class {
public:
test_class() {
}
~test_class() {
st_executor_.cancel();
if (st_thread_.joinable())
st_thread_.join();
}
void run() {
st_thread_ = std::thread([this]() {
st_executor_.spin();
});
}
private:
rclcpp::executors::SingleThreadedExecutor st_executor_;
std::thread st_thread_;
};
int main(int argc, char ** argv) {
rclcpp::init(argc, argv);
do {
test_class tc;
tc.run();
//sleep(1);
}
while (0);
std::cout << "This string is never printed" << std::endl;
}