Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions turtlesim/tutorials/teleop_turtle_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
#include <signal.h>
#include <stdio.h>

#include <atomic>
#include <functional>
#include <stdexcept>
#include <stop_token>
#include <thread>

#include <rclcpp/rclcpp.hpp>
Expand Down Expand Up @@ -61,7 +63,7 @@ static constexpr char KEYCODE_R = 0x72;
static constexpr char KEYCODE_T = 0x74;
static constexpr char KEYCODE_V = 0x76;

bool running = true;
std::atomic<bool> running = true;

class KeyboardReader final
{
Expand Down Expand Up @@ -200,7 +202,17 @@ class TeleopTurtle final
{
char c;

std::thread{std::bind(&TeleopTurtle::spin, this)}.detach();
// Spin the node on a background thread. std::jthread joins automatically
// when keyLoop() returns, and the stop_token cancels the executor cleanly,
// so the spinner is guaranteed to have stopped touching the node before it
// is destroyed -- unlike the previous detach() which raced shutdown.
std::jthread spin_thread(
[this](std::stop_token stop_token) {
rclcpp::executors::SingleThreadedExecutor executor;
executor.add_node(nh_);
std::stop_callback cancel_on_stop(stop_token, [&executor]() {executor.cancel();});
executor.spin();
});

puts("Reading from keyboard");
puts("---------------------------");
Expand Down Expand Up @@ -298,11 +310,6 @@ class TeleopTurtle final
}

private:
void spin()
{
rclcpp::spin(nh_);
}

void sendGoal(float theta)
{
using Rotate = turtlesim_msgs::action::RotateAbsolute;
Expand Down