#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
typedef int V_INT32;
typedef unsigned int V_UINT32;
struct VQueueIdx
{...}{
VQueueIdx(): idx_new( 0 ), idx_old( 0 )
{}
void resetInfo()
{...}{
idx_new = 0;
idx_old = 0;
}
bool isEmpty() const
{...}{
return ( idx_new == idx_old );
}
V_INT32 idx_new;
V_INT32 idx_old;
};
template <class Type, const int QLen>
class VQueueTemp
{...}{
public:
//从队列中取出
bool pickFromQueue( Type & save_item );
//存入队列
bool sendToQueue( const Type & new_item );
void resetQueue()
{
queue_idx.resetInfo();
}
//查询队列
const Type * lookupQueue( int idx ) const;
bool isQueueEmpty() const
{...}{
return queue_idx.isEmpty();
}
bool isQueueFull() const
{...}{
return ( queue_idx.idx_new + 1 ) % QLen == queue_idx.idx_old;
}
private:
void decQueue();
void incQueue(); 
bool readQueueItem( Type & save_item ) const;/**//*判断消息队列是否有消息, 并且读取*/
bool writeQueueItem( const Type & new_item );
int queueItems() const
{...}{
return (queue_idx.idx_new - queue_idx.idx_old + QLen)%QLen;
}
Type msg_queue[QLen];
VQueueIdx queue_idx;
};
template <class Type, const int QLen>
inline void VQueueTemp<Type, QLen>::decQueue()
{...}{
int idx = queue_idx.idx_old;
if ( ++idx >= QLen )
{
idx = 0;
}
queue_idx.idx_old = idx;
}
template <class Type, const int QLen>
inline void VQueueTemp<Type, QLen>::incQueue()
{...}{
int idx = queue_idx.idx_new;
if ( ++idx >= QLen )
{
idx = 0;
}
queue_idx.idx_new = idx;
}
template <class Type, const int QLen>
inline bool VQueueTemp<Type, QLen>::sendToQueue( const Type & new_item )
{...}{
if ( writeQueueItem(new_item) )
{
incQueue();
return true;
}
return false;
}
template <class Type, const int QLen>
inline bool VQueueTemp<Type, QLen>::pickFromQueue( Type & save_item )
{...}{
if ( readQueueItem( save_item ) )
{
decQueue();
return true;
}
return false;
}
template <class Type, const int QLen>
inline bool VQueueTemp<Type, QLen>::readQueueItem( Type & save_item ) const
{...}{
if( !isQueueEmpty() )
{
save_item = msg_queue[queue_idx.idx_old];
return true;
}
return false;
}
template <class Type, const int QLen>
inline bool VQueueTemp<Type, QLen>::writeQueueItem( const Type & new_item )
{...}{
if ( !isQueueFull() )
{
msg_queue[queue_idx.idx_new] = new_item;
return true;
}
return false;
}
template <class Type, const int QLen>
inline const Type * VQueueTemp<Type, QLen>::lookupQueue( int idx ) const
{...}{
if ( queueItems() > idx )
{
return &msg_queue[(queue_idx.idx_old+idx)%QLen];
}
return NULL;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//以上是队列实现部分
//以下是测试部分
//////////////////////////////////////////////////////////////////////////////////
struct TestData
{...}{
V_UINT32 a;
V_UINT32 b;
};
VQueueTemp<TestData, 16> testQueue;
#define TIMES 9999
#define SleepMs( ms ) ( usleep( (ms)*1000 ) )
void *thrdFunc(void*arg)
{...}{
TestData regs;
if ( arg )
{
//生产者
for ( int i=0;i < TIMES; ++i )
{
regs.b = i;
regs.a = i*3;
while ( !testQueue.sendToQueue( regs ) )
{
SleepMs(1);
}
}
}
else
{...}{
//消费者
for ( int i=0;i < TIMES; ++i )
{
while ( !testQueue.pickFromQueue( regs ) )
{
SleepMs(1);
}
if ( regs.a != 3*i || regs.b != i )
{...}{
printf( "a=%d, i=%d ", regs.a, i );
}
}
}
return NULL;
}
int main()
{...}{
pthread_t pid[2];
for ( int i = 0; i < 2; ++i )
{
//一个生产者线程,一个消费者线程
pthread_create( &pid[i], NULL, thrdFunc, (void*)i );
}
for ( int i = 0; i < 2; ++i )
{...}{
pthread_join( pid[i], NULL );
} 
return 0;
}
C++线程安全队列实现
本文介绍了一个使用C++实现的线程安全队列,该队列能够支持多线程环境中高效的消息传递。文章详细展示了队列的类定义、成员函数及其实现细节,并通过一个简单的例子演示了如何在生产者-消费者模型中使用这个队列。
10万+

被折叠的 条评论
为什么被折叠?



