Smarter std::thread ownership for HID clients

This commit is contained in:
Jack Andersen 2016-10-10 15:20:39 -10:00
parent 51a097da6b
commit 1877c546ac
2 changed files with 50 additions and 50 deletions

View File

@ -22,7 +22,7 @@ class HIDDeviceIOKit : public IHIDDevice
const std::string& m_devPath; const std::string& m_devPath;
std::mutex m_initMutex; std::mutex m_initMutex;
std::condition_variable m_initCond; std::condition_variable m_initCond;
std::thread* m_thread; std::thread m_thread;
bool _sendUSBInterruptTransfer(const uint8_t* data, size_t length) bool _sendUSBInterruptTransfer(const uint8_t* data, size_t length)
{ {
@ -228,11 +228,11 @@ public:
std::unique_lock<std::mutex> lk(m_initMutex); std::unique_lock<std::mutex> lk(m_initMutex);
DeviceToken::DeviceType dType = token.getDeviceType(); DeviceToken::DeviceType dType = token.getDeviceType();
if (dType == DeviceToken::DeviceType::USB) if (dType == DeviceToken::DeviceType::USB)
m_thread = new std::thread(_threadProcUSBLL, this); m_thread = std::thread(_threadProcUSBLL, this);
else if (dType == DeviceToken::DeviceType::Bluetooth) else if (dType == DeviceToken::DeviceType::Bluetooth)
m_thread = new std::thread(_threadProcBTLL, this); m_thread = std::thread(_threadProcBTLL, this);
else if (dType == DeviceToken::DeviceType::GenericHID) else if (dType == DeviceToken::DeviceType::GenericHID)
m_thread = new std::thread(_threadProcHID, this); m_thread = std::thread(_threadProcHID, this);
else else
{ {
fprintf(stderr, "invalid token supplied to device constructor\n"); fprintf(stderr, "invalid token supplied to device constructor\n");
@ -244,8 +244,8 @@ public:
~HIDDeviceIOKit() ~HIDDeviceIOKit()
{ {
m_runningTransferLoop = false; m_runningTransferLoop = false;
m_thread->join(); if (m_thread.joinable())
delete m_thread; m_thread.join();
} }

View File

@ -37,7 +37,7 @@ class HIDDeviceUdev final : public IHIDDevice
const std::string& m_devPath; const std::string& m_devPath;
std::mutex m_initMutex; std::mutex m_initMutex;
std::condition_variable m_initCond; std::condition_variable m_initCond;
std::thread* m_thread; std::thread m_thread;
bool _sendUSBInterruptTransfer(const uint8_t* data, size_t length) bool _sendUSBInterruptTransfer(const uint8_t* data, size_t length)
{ {
@ -245,11 +245,11 @@ public:
std::unique_lock<std::mutex> lk(m_initMutex); std::unique_lock<std::mutex> lk(m_initMutex);
DeviceToken::DeviceType dType = token.getDeviceType(); DeviceToken::DeviceType dType = token.getDeviceType();
if (dType == DeviceToken::DeviceType::USB) if (dType == DeviceToken::DeviceType::USB)
m_thread = new std::thread(_threadProcUSBLL, this); m_thread = std::thread(_threadProcUSBLL, this);
else if (dType == DeviceToken::DeviceType::Bluetooth) else if (dType == DeviceToken::DeviceType::Bluetooth)
m_thread = new std::thread(_threadProcBTLL, this); m_thread = std::thread(_threadProcBTLL, this);
else if (dType == DeviceToken::DeviceType::GenericHID) else if (dType == DeviceToken::DeviceType::GenericHID)
m_thread = new std::thread(_threadProcHID, this); m_thread = std::thread(_threadProcHID, this);
else else
{ {
fprintf(stderr, "invalid token supplied to device constructor"); fprintf(stderr, "invalid token supplied to device constructor");
@ -261,8 +261,8 @@ public:
~HIDDeviceUdev() ~HIDDeviceUdev()
{ {
m_runningTransferLoop = false; m_runningTransferLoop = false;
m_thread->join(); if (m_thread.joinable())
delete m_thread; m_thread.join();
} }