This commit is contained in:
Drath
2025-09-23 01:47:48 +02:00
parent 4dcc80171f
commit 9398e2cd8f

View File

@@ -143,18 +143,18 @@ int main(int argc, char* argv[]) {
Sensors* pSensors = new Sensors; Sensors* pSensors = new Sensors;
std::println("[main] size of sensor struct is {} bytes", sizeof(Sensors)); std::println("[main] size of sensor struct is {} bytes", sizeof(Sensors));
std::vector<std::function<void(std::stop_token st, Sensors * s)>> funcs = {displayThread, hassThread, sensorsThread}; std::function<void(std::stop_token st, Sensors * s)> funcs[] = {displayThread, hassThread, sensorsThread}; // array of functions
/* wtf is this? /* what the fucking kind of code is this
this spins up funcs as threads and halts until sMain semaphore is released in sighandler this code below spins up display/sensors/homeassistant functions as threads and halts until sem::main is released in sighandler
its scoped because when sMain is released, program waits until all the threads join back before leaving the scope its scoped because when sem::main is released, program destroys the std::jthreads and the vector before leaving the scope
threads object has to be inside the scope too because destruction of std::jthread is what actually makes threads join. this causes all threads to join back into main thread before leaving the scope, so we can safely destroy pSensors without segfault
*/ */
{ {
std::vector<std::jthread> threads; std::vector<std::jthread> threads;
for (auto& f : funcs) for (auto& f : funcs)
threads.emplace_back(f, pSensors); threads.emplace_back(f, pSensors); // params: function, stop_token (implied), pointer to sensor struct
sem::main.acquire(); sem::main.acquire();
} }