Communication
Seeing all that, you may now agree with me when I say that data sharing is hard. It is hard to do it safely as there are so many opportunities to make a mistake in the code. It is also hard to do it fast because locking approaches don’t scale well. In other words, locking will prevent the code from working two times faster when you run the code on twice the CPU cores.
Luckily, there’s a better way. Better, but way harder. Instead of sharing, we can do what I’ve been advocating since the beginning of this book and change the algorithm. And to do that, we’ll need some communication techniques. In my parallel projects, I always prefer communication to synchronization, and I strongly suggest that you try to do the same.
You may find it strange that I’ve dedicated that much space to data sharing and synchronization techniques if I don’t recommend using them. Well, sometimes you just have to bite the bullet and share data because...