31
votes

Does anyone know if there are any lock-free container libraries available for .NET ?

Preferably something that is proven to work and faster than the Synchronized wrappers we have in .NET.

I have found some articles on the .NET, but none of them specify any speed benchmarking, nor do they inspire much confidence in their reliability.

Thanks

4
Please follow up and post any performance related data you may have found on the lock free structures you might have tested. - Jason Short
We ended-up rolling our own containers, largelly based from boyet.com/index.html. Using lock-free versus standard containers we had a marginal (on average) decrease of ~12% of the total time needed to process our sample batch. All in all I would say it was not worth the effort :-( - Radu094
I came across Ariadne which has these. I'm having difficulty understanding the code though. It has some quirky empty for statements like for(;;){...some code...} and it will have a return statement inside there. It has a comment at the top stating: "This queue is mostly for completion or for use in other classes in the library, considering that the 4.0 FCL already has a lock-free queue. The Mono implementation is very close to this, while the MS implementation is more complicated but should offer better use of CPU caches...<snip>" - Louis Somers
@LouisSomers I'm not so much quirky as old-fashioned; that used to be a popular style for an infinite loop (or one that breaks on something other than conditions in the loop definition) though while(true) is perhaps more popular today the former reads to me as "for ever" while the latter as "while basic-logical-rules-of-the-universe-hold", which disturbs me slightly. I did add some functionality beyond the FCL since that comment, I should edit it, but I think I'll save that until a more thorough review later this year. - Jon Hanna
@LouisSomers I'm cool with "quirky", there are some bits of that code in that library that are indeed quirky for speed (goto in hand-coded tail-call elimination) or quirky because the state-transitions are unusual compared to what would be natural if concurrency didn't have to be considered. while(true) and for(;;) though are exactly the same, it's purely a matter of style. for(;;) is used in K&R and Stroustrup's C++ book but while(true) became more popular later on, so for(;;) is just more old-fashioned. (Also some older tools will complain about the constant, though not in C#)... - Jon Hanna

4 Answers

17
votes

Late, but better than never I thought I would add Julian Bucknalls articles to this list.

But he does not have performance numbers. In my testing of his structures the list scaled well compared to locking (very low kernel usage compared to ReaderWriterLock).

His blog has a series of articles on lock free structures in C#.

LOCK-FREE DATA STRUCTURES: THE STACK

11
votes

Do you mean the container classes like they exist in the PFX framework (Parallels for .NET), ConcurrentQueue & ConcurrentStack

Pfx blog

1
votes

Without knowing anything about it, there is one library I stumbled across here.

Though probably not quite what you are looking for, at least there is an implementation and discussion on StackOverflow of a lock free queue structure in C# here. Going through the StackOverflow code review process might give some confidence about its safety, or provide information about how to go about building your lock-free containers yourself.

-5
votes

Lock free data structures are going to have issues until they modify the CLR with the mess caused by memory models, see the CLI spec.

Lock-free programming is sufficiently difficult that you shouldn't bother with it on a collection (container) level btw. True for any language out there..