I was walking to school yesterday, standing right in front of my house, in a cross intersection, waiting for the traffic light to change, while suddenly all lights started blinking.
Nobody knew what to do, and apparently all drivers thought they should be the first to drive - and they all barged in the intersection.
It was complete chaos. Children going to school wanted to cross the street, but couldn't, everybody was blocked and honking. I called the police, not knowing who is responsible for solving it, and after about 5 minutes the lights was back and working.
The first thing that came to my mind was 'Hey, that a deadlock!' I am teaching at 'Magshimim', a cyber educating program for high-school student, preparing them to serve in intelligence units in the army, and just day before I taught them of threads.
Threads are a concept in programming, a way of letting the program ran few things at the same time. It's very useful, but one can encounter problems using it - for example - if few threads using the same resources, they need to prevent the others from changing them for the moment of use, it called 'locking' the resource.
Lets say we have two threads, they both want to use 2 resources, firstly each thread manage to get a hold of one of the resources, then they both want to use the other resource - which is being held by the other thread. Now they both are waiting for each other, and they won't continue, so they are stuck. it's called deadlock. Just like the cars in the intersection -
How can we solve it? It's not so easy, we need to make sure we use locks cleverly, and only in the specific place they are needed. And most importantly - never lock partial resource, when we lock something - make sure it's all locked.
That's for now!
For more information on threads: https://en.wikipedia.org/wiki/Thread_(computing)
How to avoid deadlock (C++):
General Threading guide (C++):