Typical code.
bool Server::QueueEmpty(void) {
bool empty;
try {
THREAD_WRITE("Checking SendQueue", YELLOW);
empty = true;
while (1) {
boost::mutex::scoped_lock lock(Queue_Lock, boost::try_to_lock);
if (lock) {
empty = SendQueue.empty();
break;
} else {
// Didn't get lock, retry after yield
THREAD_WRITE("Sendstart Yielding QueueEmpty", RED);
this_thread::yield();
THREAD_WRITE("Sendstart Returning QueueEmpty", RED);
}
}
} catch (std::exception &e) {
DebugPort.WriteLine(e.what(), RED);
empty = true;
}
return empty;
}
Is there an obvious problem with this code?
It is using Yield, which I assumed was the right way to release the CPU.
Using sleep feels wrong, since I do not want to sleep, I want to wait for the mutex.
The first thread implements the mutex in the same way, although not in a subroutine.
Am I correct in assuming, that since I use scoped_lock, the mutex will
always be released when I exit the while loop with "break;"?