Multi-Threading issue - Deadlock - in real life

in #steemstem3 years ago (edited)

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.

20180110_134424-01.jpeg

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 -

deadlock.png
(https://cs.nyu.edu/~gottlieb/courses/2000s/2000-01-spring/os/lectures/lecture-24.html)

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++):

Good luck!

Sort:  

The only solution I can think of is:
To get out and direct the movement is a policeman's job
Apparently, it took some time for a policeman to help the drivers and the children

They somehow fixed the lights, no police arrived, even though it was in city center and was very dangerous :-/

Good luck, I was also in the army

The @OriginalWorks bot has determined this post by @noogler to be original material and upvoted it!

ezgif.com-resize.gif

To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!

Please note that this is a BETA version. Feel free to leave a reply if you feel this is an error to help improve accuracy.

Build a round about that forces you out?

Could be a great idea, wonder how to implement such a thing in coding...