Implementing a lock-free queue (for a Logger component)

自古美人都是妖i 提交于 2019-12-04 17:52:06

If you find that using a lock in this case is too slow, you have a much bigger problem. A lock, when it's not contended, takes about 75 nanoseconds on my system (2.0 GHz Core 2 Quad). When it's contended, of course, it's going to take somewhat longer. But since the lock is just protecting a call to Enqueue or Dequeue, it's unlikely that the total time for a log write will be much more than that 75 nanoseconds.

If the lock is a problem--that is, if you find your threads lining up behind that lock and causing noticeable slowdowns in your application--then it's unlikely that making a lock-free queue is going to help much. Why? Because if you're really writing that much to the log, your lock-free blocking queue is going to fill up so fast you'll be limited to the speed of the I/O subsystem.

I have a multi-threaded application that writes on the order of 200 log entries a second to a Queue<string> that's protected by a simple lock. I've never noticed any significant lock contention, and processing isn't slowed in the least bit. That 75 ns is dwarfed by the time it takes to do everything else.

This implementation of a lock free queue might be helpful, where the queue is the data structure you'd use to enqueue the items to be dequeued and written out by the logger.

http://www.boyet.com/Articles/LockfreeQueue.html

You might also look at .Net 4's ConcurrentQueue

http://www.albahari.com/threading/part5.aspx#_Concurrent_Collections

http://geekswithblogs.net/BlackRabbitCoder/archive/2011/02/10/c.net-little-wonders-the-concurrent-collections-1-of-3.aspx

There's quite a few different implementations of lock-free queues out there.

My own at http://hackcraft.github.com/Ariadne/ uses a simple approach, and is open source so you can adapt it if necessary.

ConcurrerntQueue is also lock-free, and will probably serve most purposes fine, though that in Ariadne has a few members supporting other operations (like dequeuing an enumeration of the entire contents as an atomic operation, which allows for faster enumeration by a single consumer).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!