C 說話中完成環形緩沖區。本站提示廣大學習愛好者:(C 說話中完成環形緩沖區)文章只能為提供參考,不一定能成為您想要的結果。以下是C 說話中完成環形緩沖區正文
1.完成代碼:
#include #include #include #include #include #define BUFFSIZE 1024 * 1024 #define min(x, y) ((x) < (y) ? (x) : (y)) pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; struct cycle_buffer { unsigned char *buf; unsigned int size; unsigned int in; unsigned int out; pthread_mutex_t lock; }; static struct cycle_buffer *fifo = NULL; static int init_cycle_buffer(void) { int size = BUFFSIZE, ret; ret = size & (size - 1); if (ret) return ret; fifo = (struct cycle_buffer *) malloc(sizeof(struct cycle_buffer)); if (!fifo) return -1; memset(fifo, 0, sizeof(struct cycle_buffer)); fifo->size = size; fifo->in = fifo->out = 0; pthread_mutex_init(&fifo->lock, NULL); fifo->buf = (unsigned char *) malloc(size); if (!fifo->buf) free(fifo); else memset(fifo->buf, 0, size); return 0; } unsigned int fifo_get(unsigned char *buf, unsigned int len) { unsigned int l; len = min(len, fifo->in - fifo->out); l = min(len, fifo->size - (fifo->out & (fifo->size - 1))); memcpy(buf, fifo->buf + (fifo->out & (fifo->size - 1)), l); memcpy(buf + l, fifo->buf, len - l); fifo->out += len; return len; } unsigned int fifo_put(unsigned char *buf, unsigned int len) { unsigned int l; len = min(len, fifo->size - fifo->in + fifo->out); l = min(len, fifo->size - (fifo->in & (fifo->size - 1))); memcpy(fifo->buf + (fifo->in & (fifo->size - 1)), buf, l); memcpy(fifo->buf, buf + l, len - l); fifo->in += len; return len; } static void * thread_read(void *arg) { char buf[1024]; unsigned int n; pthread_detach(pthread_self()); for (;;) { memset(buf, 0, sizeof(buf)); pthread_mutex_lock(&fifo->lock); n = fifo_get(buf, sizeof(buf)); pthread_mutex_unlock(&fifo->lock); write(STDOUT_FILENO, buf, n); } printf("nnafter thread_read : %snn",buf); return NULL; } static void * thread_write(void *arg) { unsigned char buf[] = "hello world"; pthread_detach(pthread_self()); for (;;) { pthread_mutex_lock(&fifo->lock); fifo_put(buf, strlen(buf)); pthread_mutex_unlock(&fifo->lock); } return NULL; } int main(void) { int ret; pthread_t wtid, rtid; ret = init_cycle_buffer(); if (ret == -1) return ret; pthread_create(&wtid, NULL, thread_write, NULL); pthread_create(&rtid, NULL, thread_read, NULL); pthread_exit(NULL); return 0; }
1.buffer指向寄存數據的緩沖區,size是緩沖區的年夜小,in是寫指針下標,out是讀指針下標,在len和(fifo->size - fifo->in + fifo->out)之間取一個較小的值賦給len。留意,當(fifo->in == fifo->out+fifo->size)時,表現緩沖區已滿,此時獲得的較小值必定是0,前面現實寫入的字節數也全為0。另外一種界限情形是當len很年夜時(由於len是無符號的,正數對它來講也是一個很年夜的負數),這一句也能包管len取到一個較小的值,由於fifo->in老是年夜於等於fifo->out,所今後面的誰人表達式的值不會跨越fifo->size的年夜小把上一步決議的要寫入的字節數len“切開”,這裡又應用了一個技能。留意:現實分派給fifo->buffer的字節數fifo->size,必需是2的冪,不然這裡就會失足。既然fifo->size是2的冪,那末 (fifo->size-1)也就是一個前面幾位全為1的數,也就可以包管(fifo->in & (fifo->size - 1))總為不跨越(fifo->size - 1)的那一部門,和(fifo->in)% (fifo->size - 1)的後果一樣。
2.如許前面的代碼就不難懂得了,它先向fifo->in到緩沖區末尾這一塊寫數據,假如還沒寫完,在從緩沖區頭開端寫入剩下的,從而完成了輪回緩沖。最初,把寫指針後移len個字節,並前往len。
3.從下面可以看出,fifo->in的值可以從0變更到跨越fifo->size的數值,fifo->out也如斯,但它們的差不會跨越fifo->size 。
以上就是環形緩沖區域的C說話完成詳解,願望對年夜家有所贊助,感謝支撐!