From 062d0115c271205c6381118c209353c42480874a Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Tue, 19 Mar 2024 03:25:55 -0700 Subject: [PATCH] wait for thread to terminate before closing pin fd --- utility/RPi/interrupt.cpp | 6 ++++-- utility/SPIDEV/interrupt.cpp | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/utility/RPi/interrupt.cpp b/utility/RPi/interrupt.cpp index 9d6125f2..98ee2209 100644 --- a/utility/RPi/interrupt.cpp +++ b/utility/RPi/interrupt.cpp @@ -47,7 +47,8 @@ IrqChipCache::IrqChipCache() IrqChipCache::~IrqChipCache() { for (std::map::iterator i = irqCache.begin(); i != irqCache.end(); ++i) { - pthread_cancel(i->second.id); + pthread_cancel(i->second.id); // send cancel request + pthread_join(i->second, NULL); // wait till thread terminates close(i->second.fd); } irqCache.clear(); @@ -180,7 +181,8 @@ int detachInterrupt(rf24_gpio_pin_t pin) if (cachedPin == irqCache.end()) { return 0; // pin not in cache; just exit } - pthread_cancel(cachedPin->second.id); + pthread_cancel(cachedPin->second.id); // send cancel request + pthread_join(cachedPin->second.id, NULL); // wait till thread terminates close(cachedPin->second.fd); irqCache.erase(cachedPin); return 1; diff --git a/utility/SPIDEV/interrupt.cpp b/utility/SPIDEV/interrupt.cpp index bc7d1109..98a28136 100644 --- a/utility/SPIDEV/interrupt.cpp +++ b/utility/SPIDEV/interrupt.cpp @@ -25,7 +25,8 @@ struct IrqChipCache : public GPIOChipCache ~IrqChipCache() { for (std::map::iterator i = irqCache.begin(); i != irqCache.end(); ++i) { - pthread_cancel(i->second.id); + pthread_cancel(i->second.id); // send cancel request + pthread_join(i->second.id, NULL); // wait till thread terminates close(i->second.fd); } irqCache.clear(); @@ -160,7 +161,8 @@ int detachInterrupt(rf24_gpio_pin_t pin) if (cachedPin == irqCache.end()) { return 0; // pin not in cache; just exit } - pthread_cancel(cachedPin->second.id); + pthread_cancel(cachedPin->second.id); // send cancel request + pthread_join(cachedPin->second.id, NULL); // wait till thread terminates close(cachedPin->second.fd); irqCache.erase(cachedPin); return 1;