tmq_queue.h

来自「Generic template class library for messa」· C头文件 代码 · 共 58 行

H
58
字号
#ifndef __TMQ_QUEUE_H__#define __TMQ_QUEUE_H__#include <JTC/JTC.h>#include <cassert>#include "CountingSemaphore.h"template <class T, class Container>class TMQ_Queue{  public:    void post(        T& theData    )    {	theMutex.lock();	theContainer.push( theData );	theSemaphore.post();	theMutex.unlock();    }    bool get(        T& theData,         bool block = true    )    {	if ( block == true )	{	    theSemaphore.wait();	}	else	{	    bool theWaitResult;	    theWaitResult = theSemaphore.trywait();	    return theWaitResult;	}	theMutex.lock();	assert( theContainer.size() > 0 );	theData = theContainer.front();	theContainer.pop();	theMutex.unlock();	return true;    }  private:    JTCRecursiveMutex theMutex;    Container theContainer;    CountingSemaphore theSemaphore;};#endif /* __TMQ_QUEUE_H__ */

⌨️ 快捷键说明

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