3-10 6,753 views
我们知道类似优先级队列是使用heap堆栈来实现的。 优先级队列的用途我就不多说了,一般是用来做任务权重分配的。
下面priority_queue优先级库是在github.com找到的。 看了下他的源代码实现,得知他不是线程安全的。 如果要实现数据的线程安全,需要用sync lock实现全局锁,保证数据的原子性。
该文章写的有些乱,欢迎来喷 ! 另外文章后续不断更新中,请到原文地址查看更新。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#xiaorui.cc package main import ( "fmt" "github.com/gansidui/priority_queue" ) type Node struct { priority int value string } func (this *Node) Less(other interface{}) bool { return this.priority < other.(*Node).priority } func main() { q := priority_queue.New() q.Push(&Node{priority: 8, value: "8"}) q.Push(&Node{priority: 7, value: "7"}) q.Push(&Node{priority: 9, value: "9"}) q.Push(&Node{priority: 2, value: "2"}) q.Push(&Node{priority: 4, value: "4"}) q.Push(&Node{priority: 3, value: "3"}) q.Push(&Node{priority: 5, value: "5"}) x := q.Top().(*Node) fmt.Println(x.priority, x.value) for q.Len() > 0 { x = q.Pop().(*Node) fmt.Println(x.priority, x.value) } } |
对Python及运维开发感兴趣的朋友可以加QQ群 : 478476595 !!!
{ 2000人qq大群内有各厂大牛,常组织线上分享及沙龙,对高性能及分布式场景感兴趣同学欢迎加入该QQ群 }
另外如果大家觉得文章对你有些作用! 帮忙点击广告. 一来能刺激我写博客的欲望,二来好维护云主机的费用.
如果想赏钱,可以用微信扫描下面的二维码. 另外再次标注博客原地址 xiaorui.cc …… 感谢!
{ 2000人qq大群内有各厂大牛,常组织线上分享及沙龙,对高性能及分布式场景感兴趣同学欢迎加入该QQ群 }
另外如果大家觉得文章对你有些作用! 帮忙点击广告. 一来能刺激我写博客的欲望,二来好维护云主机的费用.
如果想赏钱,可以用微信扫描下面的二维码. 另外再次标注博客原地址 xiaorui.cc …… 感谢!


别倒贴,找人DDOS你!
原文地址:
转载时必须以链接形式注明原始出处及本声明。