⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 constantsizedfifopriority.h

📁 Larbin互联网蜘蛛索引系统
💻 H
字号:
// Larbin// Sebastien Ailleret// 09-11-99 -> 15-04-00/* this fifo will not grow * There are 2 different get : one has highest priority */#ifndef CONSTANTFIFOPRIORITY_H#define CONSTANTFIFOPRIORITY_H#include <assert.h>#include <pthread.h>#include "xutils/ConstantSizedFifo.h"template <class T>class ConstantSizedFifoPriority : public ConstantSizedFifo<T> { protected:  pthread_cond_t priority; public:  /* Specific constructor */  ConstantSizedFifoPriority (uint size);  /* Destructor */  virtual ~ConstantSizedFifoPriority ();  /* get the first object */  virtual T *getPriority ();  /* add an object in the fifo */  virtual void put (T *obj);  /* add an object in the fifo   * never block !!!   */  virtual void putForce (T *obj);};template <class T>ConstantSizedFifoPriority<T>::ConstantSizedFifoPriority (uint size)  : ConstantSizedFifo<T>(size) {  pthread_cond_init (&priority, NULL);}template <class T>ConstantSizedFifoPriority<T>::~ConstantSizedFifoPriority () {  delete [] tab;  pthread_mutex_destroy (&lock);  pthread_cond_destroy (&nonEmpty);  pthread_cond_destroy (&priority);}template <class T>T *ConstantSizedFifoPriority<T>::getPriority () {  T *tmp;  pthread_mutex_lock(&lock);  while (in == out) {    pthread_cond_wait(&priority, &lock);  }  tmp = tab[out];  out = (out + 1) % size;  pthread_mutex_unlock(&lock);  return tmp;}template <class T>void ConstantSizedFifoPriority<T>::put (T *obj) {  pthread_mutex_lock(&lock);  tab[in] = obj;  if (in == out) {    pthread_cond_broadcast(&priority);  } else if (in == (out+1)%size) {	pthread_cond_broadcast(&nonEmpty);  }  in = (in + 1) % size;  assert (in != out);  pthread_mutex_unlock(&lock);}template <class T>void ConstantSizedFifoPriority<T>::putForce (T *obj) {  pthread_mutex_lock(&lock);  tab[in] = obj;  if (in == out) {    pthread_cond_broadcast(&priority);  } else if (in == (out+1)%size) {    pthread_cond_broadcast(&nonEmpty);  }  in = (in + 1) % size;  assert(in != out);  pthread_mutex_unlock(&lock);}#endif // CONSTANTFIFOPRIORITY_H

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -