無鎖佇列適用場景:
兩個執行緒之間的互動資料, 乙個執行緒生產資料, 另外乙個執行緒消費資料,效率高
缺點:需要使用固定分配的空間,不能動態增加/減少長度,存在空間浪費和無法擴充套件空間問題
package main
import (
"fmt"
"reflect"
"strings"
"time"
type loopqueue struct
func (this* loopqueue)initqueue(length int, name string)bool, length)
this.length = length
this.name = name
this.start = 0
this.end = 0
return true
func (this* loopqueue)push(data inte***ce{})bool) = this.data[start]
this.start = (start+1) % this.length
return true, startvalue
func (this* loopqueue)isempty()boolelseelse{
fmt.println("poperror")
time.sleep(1e9)
//實現環形佇列
func main(){
q.initqueue(10, "test")
go create()
go consum()
for{
time.sleep(1e9)
無鎖環形佇列
環形一讀一寫佇列中,不需要擔心unsigned long溢位問題,因為溢位後自動回歸,相減值還會保留。示例一 注 max count 必須為 2 的指數,即 2,4,8,16.佇列尺寸 define max count 4096 define max mask 4095 max count 1 變數...
KFIFO無鎖佇列
linux核心中實現了以非常漂亮的無鎖佇列,在只有乙個讀者和乙個寫者的情況下,無需上鎖,而擁有執行緒安全的特性,使得效能相比於加鎖方式實現的佇列提公升數倍 kfifo的分析可見 這位作者講的很清楚 kifio可以實現無鎖佇列,但是為什麼可以實現呢,通過這種方式為什麼可以執行緒安全?換句話說,平時實現...
環形無鎖佇列
1 元素是先進先出的 由佇列的性質保證的,在環形佇列中通過對佇列的順序訪問保證 2 空間可以重複利用 因為一般的環形佇列都是乙個元素數固定的乙個閉環,可以在環形佇列初始化的時候分配好確定的記憶體空間,當進隊或出隊時只需要返回指定元素記憶體空間的位址即可,這些記憶體空間可以重複利用,避免頻繁記憶體分配...