added optional bool wait to remove() and next(), added remove(item)
This commit is contained in:
Regular → Executable
+44
-14
@@ -67,17 +67,25 @@ public:
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief remove the first item from queue.
|
* @brief remove the first item from queue.
|
||||||
* @return the item.
|
* @param wait true to wait for an item to be added to the queue, false to return NULL if no item is available.
|
||||||
|
* @return the item, or NULL if no item is available and wait was false.
|
||||||
*/
|
*/
|
||||||
T remove()
|
T remove(bool wait=true)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&m_mutex);
|
pthread_mutex_lock(&m_mutex);
|
||||||
|
|
||||||
while (m_queue.size() == 0)
|
T item;
|
||||||
pthread_cond_wait(&m_cond, &m_mutex);
|
if (wait) {
|
||||||
|
while (m_queue.size() == 0)
|
||||||
T item = m_queue.front();
|
pthread_cond_wait(&m_cond, &m_mutex);
|
||||||
m_queue.pop_front();
|
item = m_queue.front();
|
||||||
|
m_queue.pop_front();
|
||||||
|
}
|
||||||
|
else if (m_queue.size() > 0) {
|
||||||
|
item = m_queue.front();
|
||||||
|
m_queue.pop_front();
|
||||||
|
} else
|
||||||
|
item = NULL;
|
||||||
|
|
||||||
pthread_mutex_unlock(&m_mutex);
|
pthread_mutex_unlock(&m_mutex);
|
||||||
|
|
||||||
@@ -85,17 +93,39 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief return the first item from queue without remove.
|
* @brief Remove the specified item from queue.
|
||||||
* @return the item.
|
* @param item the item to remove.
|
||||||
|
* @return whether the item was removed.
|
||||||
*/
|
*/
|
||||||
T next()
|
bool remove(T item)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(&m_mutex);
|
||||||
|
int oldSize = m_queue.size();
|
||||||
|
if (oldSize > 0)
|
||||||
|
m_queue.remove(item);
|
||||||
|
int newSize = m_queue.size();
|
||||||
|
pthread_mutex_unlock(&m_mutex);
|
||||||
|
return newSize != oldSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief return the first item from queue without remove.
|
||||||
|
* @return the item, or NULL if no item is available and wait was false.
|
||||||
|
*/
|
||||||
|
T next(bool wait=true)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&m_mutex);
|
pthread_mutex_lock(&m_mutex);
|
||||||
|
|
||||||
while (m_queue.size() == 0)
|
T item;
|
||||||
pthread_cond_wait(&m_cond, &m_mutex);
|
if (wait) {
|
||||||
|
while (m_queue.size() == 0)
|
||||||
T item = m_queue.front();
|
pthread_cond_wait(&m_cond, &m_mutex);
|
||||||
|
item = m_queue.front();
|
||||||
|
}
|
||||||
|
else if (m_queue.size() > 0)
|
||||||
|
item = m_queue.front();
|
||||||
|
else
|
||||||
|
item = NULL;
|
||||||
|
|
||||||
pthread_mutex_unlock(&m_mutex);
|
pthread_mutex_unlock(&m_mutex);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user