c++ify the pointer to sensors struct

This commit is contained in:
2025-12-22 12:47:21 +01:00
parent d1baa531f8
commit d35be78720

View File

@@ -2,6 +2,7 @@
#include <chrono>
#include <csignal>
#include <functional>
#include <memory>
#include <mutex>
#include <semaphore>
#include <stop_token>
@@ -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<void(std::stop_token st, Sensors * s)> 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<Sensors> pSensors = std::make_unique<Sensors>();
std::vector<std::jthread> threads;
std::function<void(std::stop_token st, Sensors * s)> 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;
}