5 ms·
The One Commandment of multithreaded programming: 1. Thou Shalt Not touch shared data without synchronization. You must know what data is shared, and you must
by mikeash 13y ago
The One Commandment of multithreaded programming:
1. Thou Shalt Not touch shared data without synchronization.
You must know what data is shared, and you must synchronize all access to shared data (with one exception, when all access is read-only).
People try to find exceptions to this One Commandment. They think, I'm only writing one value, so what's the harm? Well, this is the harm. Follow the One Commandment and you'll be safe(r).
- awj 13y agoNo real argument here, but Haskell does encourage you to use read only data structures unless you truly need writes. That said, there's still a wide variety of clever ways in which you can find your way to multithreading hell.
- jtheory 13y agoRead-only data structures are a common idiom in Java as well (the classic example is String), and do indeed help reduce the headaches.
- windust 13y agoNot sure this is true. There is the concept of final fields, but that doesn't make objects immutable, and thus even when declaring object finals (or arrays) you can pretty easily shoot yourself in the foot by mutating them in-place.
- mbucc 13y agoFor immutable collections, guava is helpful.
- jtheory 13y agoIt is -- but you still do have to be aware if the objects in your collection are also immutable... if so, you may end up with an "immutable" list of 5 objects, and down the line it will still have those 5 objects, but the objects themselves may have completely different internal data. Referring partly to your parent comment... it's instructive to look at the String implementation. It's immutable, but they have to jump through a few hoops to do it properly... internally, a String has a private char array. An array is not immutable, so the code has to be very careful to never expose that char array externally. Because of that, calling toCharArray() will copy the internal array and return the copy -- that one's fairly obvious. But less obviously, a String constructed with a char array must also make a copy... otherwise the calling code might then modify the array after creating the String with it. So sure, immutable objects are possible, and widely encouraged in Java, but they're easy to do wrongly/incompletely.
- ddeck 13y agoAs Josh Bloch said: Classes should be immutable unless there's a very good reason to make them mutable Immutability makes life a LOT easier. A great example is the well designed joda-time vs the disaster that is the the mutable Date and Calendar core language classes.
- ScottBurson 13y agoYes. Now if only the standard Java collections were immutable...
- danjayh 13y agoMy exception: If all operations on the shared data are atomic, and always leave it in a valid state, then you can get away without explicit synchronization. Unfortunately, the only way you can really achieve this (without some sort of a synchronization API) is to use assembly code, and take advantage of architecture-specific data access mechanisms.
- mikeash 13y agoAbsolutely true. I would say that either atomics count as a form of "synchronization", or they're advanced enough that you can disregard the One Commandment once you get to that stage.
- ddeck 13y agoAlthough atomicity helps, it typically doesn't provide any visibility guarantees, so typically some other form of synchronization or memory barrier will be required.
- spullara 13y agoThe Atomic* classes in Java give you essentially direct access to these mechanisms.
- klodolph 13y agoBut you'll need memory barriers. Let's say you have two variables, x and y, satisfying x >= y at all times, and the values of x and y are never allowed to decrease. // Thread 1 atomic_inc(&x); atomic_inc(&y); // Thread 2 local_y = atomic_read(&y); local_x = atomic_read(&x); If you go through it logically, it's always in a valid state, right? No. You need explicit synchronization. If we start at x=1, y=1, then thread 2 can see x=1, y=2, which seems impossible! Or in short, your exception sucks. (By "sucks" I mean it is not sufficiently detailed enough to prevent errors.)
- rayiner 13y agoThis is only the case on architectures where atomic operations don't imply memory barriers. On x86, there is a total order on atomic operations, do another CPU will never see the write to "y" before the write to "x". Indeed on x86, there is total order on stores, so if you had two threads like that, where one thread only did reads and the other thread did updates, you can get away with an unlocked increment (x++; y++).
- jbn 13y agoHeh, my 1st commandment of multithreaded programming is: share as little as possible, isolate data in each thread and hand-off ownership between threads. With this, you will need only very little synchronization (only for queues), and you will not risk deadlock.
- rdtsc 13y ago0. Thou Shalt minimize and reduce shared mutable data as much as possible, in the design stage. Sometimes just making new copy and passing that around might not cause that much of a performance penalty vs the time spent debugging synchronization problems.
- rayiner 13y agoCorollary: don't put in locks blindly just because a datum is shared. You'll just get deadlocks instead of race conditions. Synchronization is like commas in English. People don't really know the rules, and there is danger in putting in too many as well as too few. You should never put in a lock without understanding the mechanics of the datum you're touching. Some times you won't need synchronization at all.
- mst 13y agoI would, on the whole, usually rather have a deadlock than a race since they tend to crash rather than silently corrupt state ... and the ones that cause retries rather than crashes tend to show up in the profiler. But that's an "err on the side of" rule of thumb rather than a defence of blindly locking things.