迴圈佇列 生產者與消費者模型

2021-07-27 08:29:20 字數 1623 閱讀 9017

迴圈佇列可以實現乙個生產者與乙個消費者模型, 不使用鎖。

迴圈佇列:容量大小為10

生產者執行緒:把從0遞增的整數依次push到佇列,佇列滿了就等1s(尾部不儲存,也就是最大存9個數)。

消費者執行緒:每3s從佇列pop乙個元素。

[xiongli@localhost data_struct]$ ./a.out            

push 0 at data[0]

push 1 at data[1]

push 2 at data[2]

push 3 at data[3]

push 4 at data[4]

push 5 at data[5]

push 6 at data[6]

push 7 at data[7]

push 8 at data[8]

the queue is full!

the queue is full!

the queue is full!

pop 0 at data[0]

push 9 at data[9]

the queue is full!

the queue is full!

the queue is full!

pop 1 at data[1]

push 10 at data[0]

the queue is full!

the queue is full!

the queue is full!

pull 2 at data[2]

push 11 at data[1]

the queue is full!

the queue is full!

the queue is full!

pop 3 at data[3]

push 12 at data[2]

the queue is full!

the queue is full!

the queue is full!

pop 4 at data[4]

push 13 at data[3]

the queue is full!

the queue is full!

the queue is full!

#include #include #define queue_size 10

struct enqueue queue;

int isfull()

void* push()

queue.data[queue.ntail] = i++;

printf("push %d at data[%d]\n",queue.data[queue.ntail],queue.ntail);

queue.ntail = (queue.ntail+1) % queue_size; //設定尾部

}return null;

}void* pop()

}printf("pull exit\n");

return null;

}int main()

佇列,生產者消費者模型

from multiprocessing import process,lock import os,time,json with open user w encoding utf 8 as f dic json.dump dic,f def search with open user r enco...

訊息佇列與生產者消費者模型

目錄生產者消費者模型 我們知道程序之間資料是相互隔離的,要想實現程序間的通訊 ipc機制 就必須借助於一些技術才可以,比如multiprocessing模組中的 佇列和管道,這兩種方式都是可以實現程序間資料傳輸的,由於佇列是管道 鎖的方式實現,所以我們著重研究佇列即可 佇列支援多個人從佇列的一端放入...

生產者與消費者模型

先介紹幾個函式 生產消費者模型 二者共享資料value,這裡,生產者是producter,消費者是consumer。生產者負責放物品到value中,消費者使用wait 等待生產者的通知。當得到通知後,消費者取出物品,並且用notify 通知生產者,可以再放下一批物品。例項 package cn.ed...