TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Thread-Safe Lock Free Priority Queues in Golang

92 点作者 slobdell超过 8 年前

6 条评论

fmstephe超过 8 年前
It is interesting to me how popular linked lists are for non-blocking data structures. They are often slow and always so hard to reason about and implement safely.<p>Here&#x27;s a very quick alternative<p><pre><code> push all messages into a channel IN. A goroutine reads from IN inserts into a sorted TREE. same goroutine reads max from TREE and pushes to channel OUT. workers read from OUT. </code></pre> This is a pretty mediocre implementation. But I post it because the numbers posted in this article were so low. I think we can do better with something far less sophisticated.<p><a href="https:&#x2F;&#x2F;gist.github.com&#x2F;fmstephe&#x2F;4fdc930ff180be3e92c693ad5a24e1b3" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;fmstephe&#x2F;4fdc930ff180be3e92c693ad5a2...</a><p>I timed that to write 600,000 messages concurrently and read 600,000 sequentially in 1.5 seconds. Not a perfect measurement but I&#x27;m on a train and I&#x27;m about to get to my stop.<p>If we need really high performance we can start substituting faster, and simpler, queues than Go&#x27;s channels and we can very likely improve that red-black tree that is doing the sorting. Each of these components is simpler, more likely correct, and from what I can see substantially faster than the sophisticated lock-free queue described in the article.<p>I don&#x27;t want to come across as snarky. But I am surprised by the popularity of linked-list based lock-free data structures like this.<p>EDIT: So the tone of my post came across nastier than I really intend. I quite enjoyed the article, I&#x27;d be interested how far the performance can be improved.
评论 #12616492 未加载
评论 #12614951 未加载
评论 #12614690 未加载
theaustinseven超过 8 年前
These queues are thread safe and lock-free, but they are far from efficient. My biggest issue here is that the runtime evaluations are wrong. The insert operation is definitely an average runtime of N (where N is the number of elements in the queue) and a worst case of infinity(since the insert operation is restarted if a race condition occurs, an insert operation could be repeated forever). I don&#x27;t know if I missed anything, so I could be wrong, but this implementation seems incredibly flawed.<p>That said, it is always nice to see new data-structures and I really wish there were more posts like this. I always like to see the different ways that people try to solve these problems.
评论 #12614705 未加载
评论 #12614609 未加载
bjacokes超过 8 年前
I get that this is dry subject matter and some humor helps to lighten it up, but I found the writing style distracting. Some paragraphs are dense with algorithm details, others just have a picture of a tech office, a story about running into someone on BART, or descriptions of data structures as &quot;lame&quot; or &quot;losers&quot;. Made it really tough to retrace my steps and get context from the preceding section.
评论 #12615040 未加载
评论 #12614974 未加载
Animats超过 8 年前
Can you write lock-free code in Go without assembly language support? Sometimes you need fence instructions or hardware compare-and-swap.
评论 #12615140 未加载
jalfresi超过 8 年前
Hats off, this is a GREAT post! Lots of detail and I&#x27;m way out of my depth but lots to dig into and learn from!<p>Thanks for posting!
happytrails超过 8 年前
I didn&#x27;t make it past the bar pic :(
评论 #12614483 未加载