是否存在用于多个读取或写入线程的无锁队列之类的东西? [英] Is there such a thing as a lockless queue for multiple read or write threads?

查看:19
本文介绍了是否存在用于多个读取或写入线程的无锁队列之类的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,当多个线程正在读取或写入时,是否有可能有一个无锁队列?我见过一个带有无锁队列的实现,它与一个读取和一个写入线程一起工作,但任何一个线程都不会超过一个.是否可以?我不认为它是.有没有人想证明这一点?

I was thinking, is it possible to have a lockless queue when more than one thread is reading or writing? I've seen an implementation with a lockless queue that worked with one read and one write thread but never more than one for either. Is it possible? I don't think it is. Can/does anyone want to prove it?

推荐答案

有多种算法可用,我最终实现了 An Optimistic Approach to Lock-Free FIFO Queues,通过指针标记避免ABA问题(需要x86 上的 CMPXCHG8B 指令),它在生产应用程序(用 Delphi 编写)中运行良好.(另一个版本,带有 Java 代码)

There are multiple algorithms available, I ended up implementing the An Optimistic Approach to Lock-Free FIFO Queues, which avoids the ABA problem via pointer-tagging (needs the CMPXCHG8B instruction on x86), and it runs fine in a production app (written in Delphi). (Another version, with Java code)

不过,要真正实现无锁,您还需要一个无锁内存分配器 - 请参阅 可扩展的无锁动态内存分配(在 并发构建块)或 NBMalloc(但到目前为止,我还没有使用其中之一).

Nevertheless, to be really-really lockless, you would also need a lock-free memory allocator - see Scalable Lock-Free Dynamic Memory Allocation (implemented in Concurrent Building Block) or NBMalloc (but so far, I didn't get to use one of these).

您可能还想查看 乐观无锁 FIFO 队列的答案?

这篇关于是否存在用于多个读取或写入线程的无锁队列之类的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆