fix for repeated wait in Queue::remove, better variable naming

This commit is contained in:
john30
2017-11-19 11:50:42 +01:00
parent e9092000bb
commit 0c16712c5f
Regular → Executable
+10 -7
View File
@@ -109,27 +109,30 @@ class Queue {
* @return whether the item was removed. * @return whether the item was removed.
*/ */
bool remove(T item, bool wait = false) { bool remove(T item, bool wait = false) {
bool ret = false; bool result = false;
pthread_mutex_lock(&m_mutex); pthread_mutex_lock(&m_mutex);
struct timespec t; struct timespec t;
clockGettime(&t); while (true) {
t.tv_sec++; // check thread death every second clockGettime(&t);
do { t.tv_sec++; // check thread death every second
size_t oldSize = m_queue.size(); size_t oldSize = m_queue.size();
if (oldSize > 0) { if (oldSize > 0) {
m_queue.remove(item); m_queue.remove(item);
if (m_queue.size() != oldSize) { if (m_queue.size() != oldSize) {
ret = true; result = true;
break; break;
} }
} }
if (!wait) {
break;
}
int ret = pthread_cond_timedwait(&m_cond, &m_mutex, &t); int ret = pthread_cond_timedwait(&m_cond, &m_mutex, &t);
if (ret != 0 && ret != ETIMEDOUT) { if (ret != 0 && ret != ETIMEDOUT) {
break; break;
} }
} while (wait); }
pthread_mutex_unlock(&m_mutex); pthread_mutex_unlock(&m_mutex);
return ret; return result;
} }
/** /**