4 ms·
Understand the fundamental problems/principles. Almost all problems and solutions stem from one limitation: only one writer at any given time. Immutable means
by imtringued 7y ago
Understand the fundamental problems/principles.
Almost all problems and solutions stem from one limitation: only one writer at any given time. Immutable means one writer. Locking means one writer. Splitting a list and processing each half means one writer. The actor model means one writer. All are valid solutions to your problems.
A piece of personal advice: Avoid acquiring more than one lock at the same time if possible. The solutions are usually incredibly messy. The dining philosopher problem is a pretty good example. Every single problem involving multiple "per object" locks requires a hand crafted solution to the problem. Changing the order in which forks are picked up does not generalize well to other problems.
If you have global locks you may get away with always acquiring them in the same order but if you make a single mistake you're opening yourself up to deadlocks.
- cryptica 7y agoThese are good tips but I would also add that if you can avoid shared memory altogether, then you should avoid it. If you can shard and distribute the data neatly and evenly so that each process handles a completely separate subset, then that is the most reliable long term solution. With shared memory, it's difficult to control who has access to what parts and when - and that can make it difficult to detect and analyse bottlenecks.
- dreamcompiler 7y ago> Avoid acquiring more than one lock at the same time if possible. If you know you're going to need 2 locks and you acquire both of them at the same time atomically, deadlock is impossible. The trouble happens when you acquire one lock, wait a bit, then acquire a second one. Maybe that's what you meant and I just misread it.
- allovernow 7y agoWhat about sequential locking/unlocking with consistent locking order for all resources? I was under the impression that this solved the deadlock problem but I haven't yet tried it in practice.
- JoeAltmaier 7y agoQueues are my advice. The locking is all in the queue operations. Everybody else just takes and posts work items to the queue. Can even use lockless queues.
- gpderetta 7y agohow would you implement, say, a concurrent hash map with queues?
- JoeAltmaier 7y agoPost service requests to the hashmap queue, get the response on your queue. But more likely a hashmap is too fine a granularity for queues. If a hashmap can be owned by one process, that would perform better.
- gpderetta 7y agoSometimes std::lock [1] is a solution: "Locks the given Lockable objects lock1, lock2, ..., lockn using a deadlock avoidance algorithm to avoid deadlock." Of course this only work if all locks you need to take are exposed and not hidden behind some abstraction. Then again, when they are it is hard to make sure you only take one lock at a time anyway... [1] https://en.cppreference.com/w/cpp/thread/lock https://en.cppreference.com/w/cpp/thread/lock