From 53164aa8c49673b53578d97e7c5f939dc0c582f6 Mon Sep 17 00:00:00 2001 From: Marcin Kondej Date: Wed, 2 Oct 2019 13:51:22 +0200 Subject: [PATCH] Added lambda function for mutex unlocking --- transmitter.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/transmitter.cpp b/transmitter.cpp index 9237dcf..24cf29e 100644 --- a/transmitter.cpp +++ b/transmitter.cpp @@ -466,22 +466,23 @@ void Transmitter::transmitThread() uint64_t start = current; bool locked = samplesMutex.try_lock(); - while ((!locked || !samples.size()) && transmitting) { + auto unlock = [&]() { if (locked) { samplesMutex.unlock(); } + }; + while ((!locked || !samples.size()) && transmitting) { + unlock(); std::this_thread::sleep_for(std::chrono::microseconds(1)); current = *(reinterpret_cast(&timer->low)); locked = samplesMutex.try_lock(); } if (!transmitting) { - if (locked) { - samplesMutex.unlock(); - } + unlock(); break; } std::vector loaded(std::move(samples)); - samplesMutex.unlock(); + unlock(); sampleOffset = (current - playbackStart) * sampleRate / 1000000; uint32_t offset = (current - start) * sampleRate / 1000000;