Are lockless ring-buffers technically possible? - thread-safety

I need to implement a lockless ring-buffer but I still haven't found any way of doing it without a kind of lock. I've searched the internet and all I found are ways using bits to say the memory is locked.
So, it is technically possible to do it without a lock?

Related

Performance related articles on filesystems design and implementation

I'm looking for papers/group discussions/blogs on how various filesystem implementations handled their performance issues. I can't seem to find anything good and recent, that's why I probably don't have a good list in my head of what I'm really looking for, but just to give an example: what gets cached and why? and how? and which hash functions were tried and finally used? multi-threading related issues? - what gets guarded with mutexes and what's being kept atomic for the sake of simplicity? queuing related issues? There's probably more, but as I said, I'm just starting to look into it, so I'd appreciate any comments and suggestions. Thanks.

When do I need to worry about ActiveRecord locking?

I read about optimistic locking and pessimistic locking feature of ActiveRecord and I am not using them at all in my Rails code and things seem fine. What is a good rule of thumb to know when to use them? When two people are trying to edit the same object would be one example I guess.
You need to use them, or some other solution to the same problem, when you can have concurrent changes to the same data, and where there is no clear rule for which change "wins". That is a subset of the times that two people edit the same object.
In general, optimistic locking is good when conflicts are unlikely, because they have less overhead for people: you can just edit, and very occasionally get told that you need to redo the work, because someone else changed the object.
Pessimistic locking is good when conflicts are likely, and the amount of effort to redo the work is high, and your users don't want to stay making changes for long, and they can be trusted to be polite about, eg, releasing the lock when they are done.
Both work well if, and only if, you have relatively small numbers of users interested in the same data. Both models scale poorly to large numbers of concurrent edits, because they either force the user to redo work when there is a conflict, or to wait until someone else is done before they get their work done.
When you expect conflicts, and more than a very few users operating on the same data, you want to look to more advanced approaches like CQRS to solve the concurrency problems.

Executing operations

Which is a better approach to run operations
Use Delegate
Use Action
Use Predicate
Use Func
Which once is the best in terms of performance, memory and code maintainability.
Probably not what you want to hear but,
It all depends.
Using Predicate<> is a good idea in the specific applications that it is fit for (but it is also the same as Func<T, bool>).
If you can use Func<> (or its return-less cousin Action<>) then go for it. It's always better to re-use what is already there rather than re-invent the wheel.
If all else fails, fall back on delegate. There's nothing wrong with it and it still works great.
I don't think you're going to find that any one of those consistently performs any better in terms of speed or memory consumption since their performance is going to be dictated by what code you're running inside them.
Just pick what works for your needs and move on. If there's a performance issue at some point down the road...worry about it then. Code first, optimize later.
As with most things programming, it depends. Are you trying to run async operations? Multithreaded operations? Are you dealing with events? Your name 'WPF User' suggests that you're using .NET.

Is it possible to create thread-safe collections without locks?

This is pure just for interest question, any sort of questions are welcome.
So is it possible to create thread-safe collections without any locks? By locks I mean any thread synchronization mechanisms, including Mutex, Semaphore, and even Interlocked, all of them. Is it possible at user level, without calling system functions? Ok, may be implementation is not effective, i am interested in theoretical possibility. If not what is the minimum means to do it?
EDIT: Why immutable collections don't work.
This of class Stack with methods Add that returns another Stack.
Now here is program:
Stack stack = new ...;
ThreadedMethod()
{
loop
{
//Do the loop
stack = stack.Add(element);
}
}
this expression stack = stack.Add(element) is not atomic, and you can overwrite new stack from other thread.
Thanks,
Andrey
There seem to be misconceptions by even guru software developers about what constitutes a lock.
One has to make a distinction between atomic operations and locks. Atomic operations like compare and swap perform an operation (which would otherwise require two or more instructions) as a single uninterruptible instruction. Locks are built from atomic operations however they can result in threads busy-waiting or sleeping until the lock is unlocked.
In most cases if you manage to implement an parallel algorithm with atomic operations without resorting to locking you will find that it will be orders of magnitude faster. This is why there is so much interest in wait-free and lock-free algorithms.
There has been a ton of research done on implementing various wait-free data-structures. While the code tends to be short, they can be notoriously hard to prove that they really work due to the subtle race conditions that arise. Debugging is also a nightmare. However a lot of work has been done and you can find wait-free/lock-free hashmaps, queues (Michael Scott's lock free queue), stacks, lists, trees, the list goes on. If you're lucky you'll also find some open-source implementations.
Just google 'lock-free my-data-structure' and see what you get.
For further reading on this interesting subject start from The Art of Multiprocessor Programming by Maurice Herlihy.
Yes, immutable collections! :)
Yes, it is possible to do concurrency without any support from the system. You can use Peterson's algorithm or the more general bakery algorithm to emulate a lock.
It really depends on how you define the term (as other commenters have discussed) but yes, it's possible for many data structures, at least, to be implemented in a non-blocking way (without the use of traditional mutual-exclusion locks).
I strongly recommend, if you're interested in the topic, that you read the blog of Cliff Click -- Cliff is the head guru at Azul Systems, who produce hardware + a custom JVM to run Java systems on massive and massively parallel (think up to around 1000 cores and in the hundreds of gigabytes of RAM area), and obviously in those kinds of systems locking can be death (disclaimer: not an employee or customer of Azul, just an admirer of their work).
Dr Click has famously come up with a non-blocking HashTable, which is basically a complex (but quite brilliant) state machine using atomic CompareAndSwap operations.
There is a two-part blog post describing the algorithm (part one, part two) as well as a talk given at Google (slides, video) -- the latter in particular is a fantastic introduction. Took me a few goes to 'get' it -- it's complex, let's face it! -- but if you presevere (or if you're smarter than me in the first place!) you'll find it very rewarding.
I don't think so.
The problem is that at some point you will need some mutual exclusion primitive (perhaps at the machine level) such as an atomic test-and-set operation. Otherwise, you could always devise a race condition. Once you have a test-and-set, you essentially have a lock.
That being said, in older hardware that did not have any support for this in the instruction set, you could disable interrupts and thus prevent another "process" from taking over but effectively constantly putting the system into a serialized mode and forcing sort of a mutual exclusion for a while.
At the very least you need atomic operations. There are lock free algorithms for single cpu's. I'm not sure about multiple CPU's

Is it possible to implement a GC.GetAliveInstancesOf<T>() (for use in debugging)?

I know this was answered before, but I'd like to pose a somewhat different question.
Is there any conceivable way to implement GC.GetAliveInstancesOf(), that can be evaluated in Visual Studio Debug Watch window? Sasha Goldstein shows one solution in this article, but it requires every class you want to query inherit from a specific base class.
I'll emphasize that I only want to use this method during debugging, so I don't care that the GC may change an object's address in memory during runtime.
One idea might be to somehow harness the !dumpheap –type command of SOS, and do some magic trick to create a temporary variable and have it point out to the memory address printed by SOS.
Does anyone have a solution that works?
It looks like something very similar to this was implemented by Alois Kraus in the WMemoryProfiler project.
There is just about nothing that could drag down the garbage collector more than it having to second-guess that some kind of user code could be interested in finding roots that it doesn't own. Keeping it as snappy as possible is crucial. For that matter, the only way you could ever see what is being referenced with some kind of confidence is to freeze all threads that might be allocating memory from the garbage collected heap.
Well, that's possible, debuggers do that. You already know the way Windbg does it. It however wasn't designed to be a tool that was optimized to track managed objects. There are other tools that were: memory profilers. Plenty to choose from, don't try to build your own. From the freeware (and time-wasting) CLR Profiler, to the 3rd party offerings like Ants and dotTrace and many others. A couple of hundred bucks to solve your problem, there is no way you can do better by yourself for less.

Resources