多執行緒 佇列

2021-06-05 07:35:26 字數 749 閱讀 4985

對於編寫多執行緒的朋友來說,佇列具有天生的互斥性。在佇列裡面,乙個負責新增資料,乙個負責處理資料。誰也不妨礙誰,誰也離不開誰。所以,佇列具有天生的並行性。

只針對:乙個執行緒讀,乙個執行緒寫。當不滿足這個先決條件,多執行緒也完蛋,也得進佇列加鎖,出佇列加鎖

view plain

print?

#define max_number 1000l

#define status int

#define ok     0

#define false -1

typedef

struct _queue_data  

queue_data;   

此時,乙個執行緒壓入資料,操作為push_data,

view plain

print?

status push_data(queue_data* pqueue, int data)    

那麼,還有乙個執行緒就負責處理資料pop_data,

view plain

print?

status pop_data(queue_data* pqueue, int* pdata)    

總結:

(1)佇列只適合兩個執行緒並行使用,乙個壓入資料,乙個彈出資料

(2)佇列是沒有鎖的並行,沒有死鎖的危險

(3)佇列中head和tail只有在計算結束之前的時候才能進行自增運算

多執行緒, 執行緒佇列

self performselectoronmainthread selector refreshcellforliveid withobject userinfo waituntildone yes 該方法的作用是在主線程中,執行制定的方法 塊 引數 selector refreshcellfor...

多執行緒 阻塞佇列

blockingqueue最終會有四種狀況,丟擲異常 返回特殊值 阻塞 超時,下表總結了這些方法 丟擲異常 特殊值阻塞 超時插入add e offer e put e offer e,time,unit 移除remove poll take poll time,unit 檢查element peek...

多執行緒 阻塞佇列

1 當佇列滿的時候,插入元素的執行緒被阻塞,直達佇列不滿。2 隊列為空的時候,獲取元素的執行緒被阻塞,直到佇列不空。生產者就是生產資料的執行緒,消費者就是消費資料的執行緒。在多執行緒開發中,如果生產者處理速度很快,而消費者處理速度很慢,那麼生產者就必須等待消費者處理完,才能繼續生產資料。同樣的道理,...