From c50c44cc582956aefd32e39a6c7a5210ba4e1585 Mon Sep 17 00:00:00 2001 From: Arkadiusz Niemiec <niemiecarkadiusz@gmail.com> Date: Sat, 20 Apr 2019 21:30:04 +0200 Subject: [PATCH] Add a boolean to cleanly exit sender thread --- msg/templates/urtps/microRTPS_agent.cpp.template | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/msg/templates/urtps/microRTPS_agent.cpp.template b/msg/templates/urtps/microRTPS_agent.cpp.template index d2db7d8f6f..5249f58369 100644 --- a/msg/templates/urtps/microRTPS_agent.cpp.template +++ b/msg/templates/urtps/microRTPS_agent.cpp.template @@ -53,6 +53,7 @@ recv_topics = [s.short_name for idx, s in enumerate(spec) if scope[idx] == MsgSc ****************************************************************************/ #include <thread> +#include <atomic> #include <unistd.h> #include <poll.h> #include <chrono> @@ -181,8 +182,9 @@ void signal_handler(int signum) running = 0; transport_node->close(); } -@[if recv_topics]@ +@[if recv_topics]@ +std::atomic<bool> exit_sender_thread(false); void t_send(void *data) { char data_buffer[BUFFER_SIZE] = {}; @@ -192,7 +194,7 @@ void t_send(void *data) while (running) { // Send subscribed topics over UART - while (topics.hasMsg(&topic_ID)) + while (topics.hasMsg(&topic_ID) && !exit_sender_thread.load()) { uint16_t header_length = transport_node->get_header_length(); /* make room for the header to fill in later */ @@ -301,6 +303,7 @@ int main(int argc, char** argv) usleep(_options.sleep_us); } @[if recv_topics]@ + exit_sender_thread = true; sender_thread.join(); @[end if]@ delete transport_node; -- GitLab