环形缓冲区高效的执行(包括线程安全的,而不是线程安全的) [英] CircularBuffer highly efficient implementation (both thread-safe and not thread-safe)

查看:316
本文介绍了环形缓冲区高效的执行(包括线程安全的,而不是线程安全的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能提出好的环形缓冲区实现?我既需要不是线程安全和线程安全的版本。我期待下面的操作:

Could someone suggest good CircularBuffer implementation? I need both "not thread-safe" and "thread-safe" versions. I expect following operations:


  • 创建

  • 当添加元素来提供的缓冲区大小的能力

  • 迭代元素

  • 删除元素,而迭代

  • 可能删除元素

  • ability to provide size of the buffer when creating
  • adding elements
  • iterating elements
  • removing elements while iterating
  • probably removing elements

我预计实施在速度方面的高度优化,使用的内存,一般和最差的时间等。

I expect implementation to be highly optimized in terms of speed and used memory, average and worst times etc.

我期望不是线程安全的实施是非常快的。我希望线程安全的实施要快,可能使用无锁码同步和它的确定,如果这是必需的速度有一定的限制。

I expect "not thread-safe" implementation to be extremely fast. I expect "thread-safe" implementation to be fast, probably using "lock-free code" for synchronization and it's ok to have some restrictions if this is required for speed.

如果缓冲区太小,无法存储新(加)元素它的确定silenty覆盖存在的元素或引发异常。

If buffer is too small to store new (added) element it's ok to silenty override existent element or raise exception.

我应该使用disruptor.net?

Should I use disruptor.net?

添加链接到一个很好的例子 Disruptor.net例

Adding link to a good example Disruptor.net Example

推荐答案

不是线程安全的:

System.Collections.Generic.Queue

线程安全的:

系统.Collections.Concurrent.ConcurrentQueue

System.Collections.Concurrent.BlockingCollection (它在内部默认使用并发队列)

System.Collections.Concurrent.BlockingCollection (which uses a concurrent queue by default internally)

虽然技术上你真的不应该使用线程安全。这简直太暧昧了。首先是没有设计为被多个线程同时使用,其余的都是。

Although technically you really shouldn't use the term "thread safe". It's simply too ambiguous. The first is not designed to be used concurrently by multiple threads, the rest are.

这篇关于环形缓冲区高效的执行(包括线程安全的,而不是线程安全的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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