美文网首页
C环形队列

C环形队列

作者: hades2013 | 来源:发表于2018-06-04 13:51 被阅读0次

环形队列是什么

队列是一种常用的数据结构,这种结构保证了数据是按照“先进先出”的原则进行操作的,即最先进去的元素也是最先出来的元素.环形队列是一种特殊的队列结构,保证了元素也是先进先出的,但与一般队列的区别是,他们是环形的,即队列头部的上个元素是队列尾部,通常是容纳元素数固定的一个闭环。

环形队列的工作场景

一般应用于需要高效且频繁进行多线程通信传递数据的场景,例如:linux捕包、发包等等,(linux系统中对PACKET_RX_RING和PACKET_TX_RING的支持实质就是内核实现的一种环形队列)

#include <assert.h>
#include <string.h>

typedef unsigned char u_char;

#define CAN_WRITE 0x00
#define CAN_READ 0x01
#define READING 0x02
#define WRITING 0x03

typedef struct tag
{
    u_char tag_value;
}TAG;


class Ring_Queue
{
    public:
        Ring_Queue(int nmemb,int size):_nmemb(nmemb),_size(size)
                                         ,_read_now(0),_write_now(0)
    {
        if ( nmemb <= 0 || size <=0 )
        {
            assert(0);
        }
        _queue_p = NULL;
        _queue_p = new u_char[ nmemb * (sizeof(TAG) + size)];
        memset(_queue_p,0,nmemb * (sizeof(TAG) + size));

    }
        ~Ring_Queue()
        {
            if (_queue_p) delete []_queue_p;

        }
        u_char * SOLO_Read()
        {
            u_char * g_p = 0;
            TAG * tag_p = 0;
            u_char *user_data = 0;

            g_p = queue_peek_nth(_queue_p,_read_now);
            tag_p = (TAG *)g_p;
            if (tag_p->tag_value == CAN_READ)
            {
                user_data = (u_char *)g_p + sizeof(TAG);
                tag_p->tag_value = READING;
            }
            return user_data;
        }
        void SOLO_Read_Over()
        {
            u_char * g_p = 0;
            TAG * tag_p = 0;

            g_p = queue_peek_nth(_queue_p,_read_now);
            tag_p = (TAG *)g_p;
            if (tag_p->tag_value == READING)
            {
                tag_p->tag_value = CAN_WRITE;
                _read_now = (_read_now + 1)% _nmemb;
            }
        }
        u_char * SOLO_Write()
        {
            u_char * g_p = 0;
            TAG * tag_p = 0;
            u_char *user_data = 0;

            g_p = queue_peek_nth(_queue_p,_write_now);
            tag_p = (TAG *)g_p;
            if (tag_p->tag_value == CAN_WRITE)
            {  
                user_data = (u_char *)g_p + sizeof(TAG);
                tag_p->tag_value = WRITING;
            }                
            return user_data;
        }
        void SOLO_Write_Over()
        {
            u_char * g_p = 0;
            TAG * tag_p = 0;

            g_p = queue_peek_nth(_queue_p,_write_now);
            tag_p = (TAG *)g_p;
            if (tag_p->tag_value == WRITING)
            {
                tag_p->tag_value = CAN_READ;
                _write_now = (_write_now + 1)% _nmemb;
            }
        }
    private:
        u_char *queue_peek_nth(u_char *queue_p,int pos)
        {
            u_char *rst = 0;
            if (queue_p && pos < _nmemb)
            {
                rst = queue_p + pos * (sizeof(TAG) + _size);
            }
            return rst;
        }
        u_char * _queue_p;
        int _nmemb;
        int _size;
        volatile int _read_now;
        volatile int _write_now;
};
#include <stdio.h>
#include "ring_queue.h" 
#include <unistd.h>
#include <sys/time.h>

const int LOOP_SIZE = 10000;

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>


#define THREAD_NUM 1

#define DATA_TYPE int

void *customer(void *arg)
{
    Ring_Queue queue = *(Ring_Queue *)arg;

    while(1)
    {
        for(int i = 0;i < LOOP_SIZE; )
        {
            int *p = 0;
            p = (DATA_TYPE *)queue.SOLO_Read();
            if (p)
            {
                assert(i == *p);
//              printf("[%d]:%d\n",i,*p);
                queue.SOLO_Read_Over();
                i++;
            }
        }
    }
}

void *producer(void *arg)
{
    Ring_Queue queue = *(Ring_Queue *)arg;
    
    int loop = 0;
    struct timeval last_time,now_time;
    gettimeofday(&last_time,NULL);
    gettimeofday(&now_time,NULL);
    while(1)
    {
        for(int i = 0;i < LOOP_SIZE; )
        {
            int *p = 0;
            p = (DATA_TYPE *)queue.SOLO_Write();
            if (p)
            {
                *p = i;
                queue.SOLO_Write_Over();
                i++;
            }
        }
        gettimeofday(&now_time,NULL);
        if (now_time.tv_sec - last_time.tv_sec >= 5)
        {
            printf("LOOP_COUNT is %.2f=[ %d[RING_SIZE] * %.2f[RING_COUNT]] per second\n",(LOOP_SIZE*loop)/5.0,LOOP_SIZE,loop/5.0);
            last_time = now_time;
            loop = 0;
        }
        loop++;
    }
}

int main(int argc,char *argv[])
{
    pthread_t tid_customer[THREAD_NUM];
    pthread_t tid_producer[THREAD_NUM];
    Ring_Queue *queue = new Ring_Queue[THREAD_NUM](LOOP_SIZE,sizeof(DATA_TYPE));

    for (int i = 0; i < THREAD_NUM; i++)
    {
        if (pthread_create(&tid_customer[i],NULL,&customer,(void*)&queue[i]) != 0)
        {
            fprintf(stderr,"thread create failed\n");
            return -1;
        }
    }
    for (int i = 0; i < THREAD_NUM; i++)
    {
        if (pthread_create(&tid_producer[i],NULL,&producer,(void*)&queue[i]) != 0)
        {
            fprintf(stderr,"thread create failed\n");
            return -1;
        }
    }

    for (int i = 0 ;i < THREAD_NUM; i++)
        pthread_join(tid_customer[i],NULL);
    
    for (int i = 0 ;i < THREAD_NUM; i++)
        pthread_join(tid_producer[i],NULL);

}

相关文章

  • C环形队列

    环形队列是什么 队列是一种常用的数据结构,这种结构保证了数据是按照“先进先出”的原则进行操作的,即最先进去的元素也...

  • C++数据结构探险——队列篇

    数据结构的原理 队列:先进先出(FIFO:first in first out) 普通队列: 环形队列: 以C++...

  • 环形缓冲区

    下面引用维基百科,来学习环形缓冲区。 环形缓冲器 圆形缓冲区(circular buffer),也称作圆形队列(c...

  • 数组实现环形队列

    利用数组结构在队列的基础下用取模算法来实现环形队列。 环形队列拥有复用功能。 主要算法思想:1)front指向队列...

  • 2020-11-10-数据结构与算法-14(队列scala版)

    1.队列 非环形实现 2.队列环形版 核心思想: 用%来模拟循环(rear +1) /maxsize = firs...

  • 环形队列

    写完这篇文章想着以后尽量(应该说一定)使用现在正在使用的LPC系列的单片机写程序,其实内心感觉还是LPC做的相当完...

  • 环形队列

    1、概念 环形队列是一个最为简单的数据结构,底层用数组组成,然后逻辑上数组首尾相连。虽然他的结构极为简单,但是用处...

  • 环形队列

    利用数组通过取模的方式实现环形队列,使队列达到复用的效果。 图1中,rear和front分别代表队尾和队头并且初始...

  • 数据结构

    稀疏数组 代码实现: 队列 代码实现: 环形队列 什么时候队列满:(rear+1)%maxsize == fron...

  • 简单实用的环形队列(C语言)

网友评论

      本文标题:C环形队列

      本文链接:https://www.haomeiwen.com/subject/ptaesftx.html