Slay dragons and master concurrency

Recently I came across a great application built by Petr Hudeček and Michal Pokorný at HackCambridge 2016.

It is a tutorial on how to learn the inner mechanism of concurrency in software development. And also, it is a funny game where you are in the role of a powerful Scheduler, trying to beat the enemy army and their dragons. In order to win, you must cause a deadlock or an exception by releasing and stopping threads in an evil way.

The game uses C# as a programming language, but do not worry. It is quite understandable for every programmer who is at least a little familiar with the syntax of the C language.

One of the game tutorials

The Deadlock Empire is a perfect way to learn why concurrency programming is so hard to understand and to do it right. I find that bugs caused by concurrent computing are one of the most challenging and annoying issues I experience in my daily coding. Considering that, I really appreciate that the game focuses on multi-threaded programming, which is, in my opinion, much harder to comprehend than asynchronous or parallelism techniques.

Moreover, it may be an excellent way to show our non-technical friends or managers what concurrency programming is and why the software world spends such a considerable amount of time and money to solve this jinxed and notorious puzzle.

As far as I know, I suspect the nitty-gritty of that is related to a famous classic problem with leaky abstraction coined by Joel Spolsky.

Personally, I split the abstraction leakage into two general categories:

  1. Semantically correct but slow

2) Semantically incorrect but fast.

The first kind is, for instance, when you join SQL tables. This operation is semantically correct, although sometimes it might be painfully slow.

Our topic, unfortunately, belongs to the second category. It is about misleading language constructs in the code. You see a simple statement, yet under the hood, it does not work how you would expect. Actually, rarely are there so many hard-to-find bugs in other areas of software development.

Here is a simple example to illustrate the issue:

The code:

The formula above commands you to increase the class field named “a”, which many developers are likely to interpret as an atomic operation. Interestingly enough, it is not true. Hitting the “Expand” button in the game will reveal that the super-simple statement consists of two steps:

And due to these two steps, something horrible and “unpredictable” might happen in the multi-threaded world. For example, the two “a + 1” statements in this case can result in number two, not three.

You can experience the notorious issue here.

Not surprisingly, it can cause many hidden bugs. Therefore, you have to be meticulous about details because a great deal of caveats and dangers can cross your path to a well-working code. The next problem is that you need to pay attention to two or more things that might happen at the “same time.” It is as if you were to hold three basketballs while riding a bicycle. It is multitasking, and multitasking is pretty hard, as we know.

There is no guarantee that finishing the Deadlock Empire successfully will prevent you from making mistakes in the future. Still, however, it can introduce you to various asynchronous primitives designed to help us while, simultaneously, revealing their hidden caveats and tricky parts.Solving Empire puzzles will assist you in developing your sixth sense to expose potential challenges rather than provide a comprehensive training in multi-threading techniques.

Several levels of the game are pretty tough, but I want to encourage you to complete it anyway. You will gain more insights into how difficult it is to synchronize threads and what can go wrong in your code. Despite my extensive experience in parallelism and asynchronous programming, I keep stumbling across surprising things there. You will also learn about locks, Mutexes, Deadlock, ManualResetEventSlim, expanding variables, etc.

Furthermore, the last section, the final part of the game, will require you to use all the magic and skills you have learned so far.

Spending an hour within the game will make you significantly more familiar with problems related to threads and concurrency.

Additionally, you may enjoy a little story about Scheduler, and the evil invader Parallel Wizard from the Deadlock Empire. The geek inside me was so pleased when I had to go through these battles:) So put yourself on your armor and try to defeat the Lord Parallel!

I would love to see more games like this one. If you know any, write them in the comments please. Bye for now, it is high time for me to prepare for the final fight!

One day I will love C#. I promise. (web, C#, F#, SQL, JS)