Skip to content

Commit

Permalink
push code
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Dec 9, 2016
1 parent 36b5aea commit e98b76c
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALL:
g++ -g -o ring_test main.c -lpthread -fpermissive
clean:
rm -f ring_test
100 changes: 100 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#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

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

while(1)
{
for(int i = 0;i < LOOP_SIZE; )
{
int *p = 0;
p = (int *)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 = (int *)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,4);

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);

}
108 changes: 108 additions & 0 deletions ring_queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#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;
};

0 comments on commit e98b76c

Please sign in to comment.