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

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

问题描述

我当时在想,当多个线程正在读写时,是否可能会有一个无锁队列?我见过一个带有无锁队列的实现,该队列可处理一个读取线程和一个写入线程,但对于任何一个线程都不会超过一个.是否有可能?我认为不是.可以/想要证明吗?

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?

推荐答案

有多种算法可用,我最终实现了另一个版本,带有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天全站免登陆