Whenever I’m on a long driving, I unavoidably start think about multithreading. It just seems to me that there are some good practical analogies between the two. Here’s some basics “hardware architecture” of what I mean:
| Traffic Entitiy |
Programming Equivalent |
| Single Lane Road |
Single Core CPU |
| Train |
Single Core CPU with some serious SIMD |
| Two Lane Road |
Dual Core CPU |
| X Lane Road |
X Core CPU |
| Cars |
Instructions |
| People, Luggage |
Data |
| Planes, Boats, Etc. |
Network Communications (data, instructions, queries, etc.) |
| Speed Limit |
CPU Speed (And lots of people overclock both…) |
| Parking Space/Lot/Garage |
RAM or Hard drive (depending on where you’re parking) |
Ok… so that’s the basic idea… not a whole lot of value yet. Let’s examine a few other, perhaps, “software” techniques of traffic:
| Traffic Construct |
Programming Technique |
| Lane Switching |
Locking to exchange data is probably the best analogy. When traffic is heavy, the overhead of the switching seems to really slow traffic down. Alternatively, when traffic is light you don’t really notice the overhead of the lane change (it’s also a lot easier to perform). This lane switching is obviously needed since most roads have a “general purpose” of getting you to many places. |
| Traffic Lights |
A Sync Point for traffic so that two separate lines of traffic (threads of execution) can exchange across some common section of road (data). |
| Roundabout |
An alternative to Traffic Lights, this probably most analogous to Lockless Programming Algorithms. This could even be the drive through at a fast food restaurant, and many people choose it over parking. |
| Lane Change Restrictions |
Data Parallelism by removing the number of lane changes (Locks) and ensuring that the cars ideally don’t slow each other down.
|
| HOV Lanes |
Data Parallelism that is taking advantage of SIMD.
|
There are a lot more analogies between computer science and traffic (perhaps a toll booth represents security validation), but that’s a good amount of the ones related to multithreading. Some of these problems were solved practically in traffic before we even encountered them in computer science. I’m not sure that traffic is ahead anymore (and traffic seems full of hacks anyway… perhaps another similarity with some multithreaded code), but perhaps there are still thing we could learn. And maybe one day, traffic designers will look at computer science and realize more efficient ways to get traffic around (but then when would I find time to think up such absurd analogies)… well that’s probably some wishful thinking. At least it is a little easier for us to change our code than it is for construction workers to reroute roads.