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

筅森魡賤 提交于 2019-12-21 21:58:16

问题


I am designing a new improved Logger component (.NET 3.5, C#).

I would like to use a lock-free implementation.

Logging events will be sent from (potentially) multiple threads, although only a single thread will do the actual output to file/other storage medium.

In essence, all the writers are * enqueuing* their data into some queue, to be retrieves by some other process (LogFileWriter).

Can this be achieved in a lock-less manner? i could not find a direct reference to this particular problem on the net.


回答1:


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.




回答2:


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




回答3:


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).



来源:https://stackoverflow.com/questions/8735800/implementing-a-lock-free-queue-for-a-logger-component

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