From a64d3c282f9f7587d6e54b2c19b4e67b4ad4e542 Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Sun, 29 Sep 2019 13:01:13 +0200 Subject: [PATCH] Improve thread management --- src/algorithms/PVT/libs/rtcm.cc | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/algorithms/PVT/libs/rtcm.cc b/src/algorithms/PVT/libs/rtcm.cc index e17693f91..f538792b4 100644 --- a/src/algorithms/PVT/libs/rtcm.cc +++ b/src/algorithms/PVT/libs/rtcm.cc @@ -40,7 +40,6 @@ #include #include #include // for std::reverse -#include // std::chrono::seconds #include // for std::fmod, std::lround #include // for strtol #include // for cout @@ -89,12 +88,9 @@ void Rtcm::run_server() std::cout << "Starting a TCP/IP server of RTCM messages on port " << RTCM_port << std::endl; try { - std::thread tq([&] { std::make_shared(io_context, rtcm_message_queue, RTCM_port)->do_read_queue(); }); - tq.detach(); - - std::thread t([&] { io_context.run(); }); + tq = std::thread([&] { std::make_shared(io_context, rtcm_message_queue, RTCM_port)->do_read_queue(); }); + t = std::thread([&] { io_context.run(); }); server_is_running = true; - t.detach(); } catch (const std::exception& e) { @@ -112,10 +108,11 @@ void Rtcm::stop_service() void Rtcm::stop_server() { std::cout << "Stopping TCP/IP server on port " << RTCM_port << std::endl; - rtcm_message_queue->push("Goodbye"); // this terminates tq Rtcm::stop_service(); servers.front().close_server(); - std::this_thread::sleep_for(std::chrono::seconds(1)); + rtcm_message_queue->push("Goodbye"); // this terminates tq + tq.join(); + t.join(); server_is_running = false; }