【例子】线程安全的共享缓冲队列

#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;
}
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值