The nightmare matrix

My theorem to match Euclid’s useless theorem. If you can find no path from upper OR left edge, to the right OR lower edge, where the 2 threads represented by the rows and columns, interferes with the valid state of the object, then it is thread-safe. It has to be a valid path. Meaning a synchronized block like “lock” cannot have a path changing direction from across to down, or vice versa.

Same piece of code. 2 threads. One whose timeframe is represented running horizontally. One whose timeframe is represented running vertically.

When code is executed simultaneously, it can be represented by drawing a path using lines down (thread 1 completed instruction or step), line to right (thread 2 completed instruction or step), diagonal line to down-right for a simultaneous change of state of the thread. The gaps between code should be interpreted as completed previous instruction, has not yet moved to next instruction.

I think this metaphor works. Single core, only horizontal and vertical lines. Multi-core, I think it’s possible for 2 threads to change state simultaneously running separately on separate 2 cores. The red line is just a sample path. I’m not saying it’s not thread-safe execution order, for the code in question. But you should be able to draw a path and say “That isn’t thread-safe” and to fix it.

Leave a Reply

Your email address will not be published. Required fields are marked *