From d35be78720fcd0a0f03ee31f3cab91283122be24 Mon Sep 17 00:00:00 2001 From: Drath Date: Mon, 22 Dec 2025 12:47:21 +0100 Subject: [PATCH] c++ify the pointer to sensors struct --- main.cpp | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/main.cpp b/main.cpp index 5ab61f1..25cbff0 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -168,24 +169,16 @@ int main() { std::signal(SIGINT, sighandler); std::signal(SIGTERM, sighandler); - Sensors* pSensors = new Sensors; - log(LOG::DBG, "main", "main thread pid is {}, size of sensor struct is {} bytes", gettid(), sizeof(Sensors)); - - std::function funcs[] = {displayThread, hassThread, sensorsThread}; // array of functions - - /* what the fucking kind of code is this - this code below spins up display/sensors/homeassistant functions as threads and halts until sem::main is released in sighandler - then the program leaves the scope, but before that, it destroys the std::jthreads in the "threads" vector - this causes all threads to gracefully close and join back into main thread, so we can safely destroy pSensors without segfault */ - { + std::unique_ptr pSensors = std::make_unique(); std::vector threads; + std::function funcs[] = {displayThread, hassThread, sensorsThread}; + for (auto& f : funcs) - threads.emplace_back(f, pSensors); // params: function, stop_token (implied), pointer to sensor struct + threads.emplace_back(f, pSensors.get()); // params: function, stop_token (implied), pointer to sensor struct sem::main.acquire(); } - delete pSensors; return 0; }