consta~2.h

来自「一百个病毒的源代码 包括熊猫烧香等 极其具有研究价值」· C头文件 代码 · 共 97 行

H
97
字号
// 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 + =
减小字号Ctrl + -
显示快捷键?