class WQueue: method 'T remove(const long delay)' added.

This commit is contained in:
Roland Jax
2014-06-20 13:35:16 +02:00
parent 56c5dc668c
commit ffbdf5f8b3
4 changed files with 75 additions and 27 deletions
+32 -1
View File
@@ -20,8 +20,11 @@
#ifndef WQUEUE_H_
#define WQUEUE_H_
#include <pthread.h>
#include <list>
#include <ctime>
#include <pthread.h>
#include <sys/time.h>
#include <errno.h>
template <typename T> class WQueue
{
@@ -64,6 +67,34 @@ public:
return item;
}
T remove(const long delay)
{
struct timeval tv;
struct timezone tz;
struct timespec timeout;
int ret = 0;
pthread_mutex_lock(&m_mutex);
gettimeofday(&tv, &tz);
timeout.tv_sec = tv.tv_sec + delay;
timeout.tv_nsec = tv.tv_usec * 1000;
while (m_queue.size() == 0 && ret != ETIMEDOUT)
ret = pthread_cond_timedwait(&m_condv, &m_mutex, &timeout);
if (ret == ETIMEDOUT)
return NULL;
T item = m_queue.front();
m_queue.pop_front();
pthread_mutex_unlock(&m_mutex);
return item;
}
int size()
{
pthread_mutex_lock(&m_mutex);