Hands-On Enterprise Application Development with Python
上QQ阅读APP看书,第一时间看更新

Common pitfalls with multithreading

Multithreading provides a lot of benefits but also comes with some pitfalls. These pitfalls, if not avoided, can prove to be a painful experience when the application goes into production. These pitfalls usually result in unexpected behaviors that may take place only once in a while, or may occur on every execution of a particular module. The painful thing about this is it is really hard to debug these problems when they are caused by the execution of multiple threads, since it is quite hard to predict when a particular thread will execute. So, it makes it worthwhile to discuss why these common pitfalls occur and how they can be avoided during the development stage itself.

Some of the common reasons for unexpected behaviors in multithreaded applications are race conditions and deadlocks. Let's take a look at them individually and understand why they happen and how can they be avoided.