问题描述
我当时在想,当多个线程正在读写时,是否可能会有一个无锁队列?我见过一个带有无锁队列的实现,该队列可处理一个读取线程和一个写入线程,但对于任何一个线程都不会超过一个.是否有可能?我认为不是.可以/想要证明吗?
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?
推荐答案
有多种算法可用,我最终实现了无锁FIFO队列的乐观方法,它通过指针标记避免了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队列的答案?
这篇关于是否存在诸如多个读取或写入线程的无锁队列之类的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!