Ridwan Yusuf
2 min readNov 18, 2022

Race Condition and Dead Lock Part 3

Multithreading and Multiprocessing, as mentioned in Part 2 of this series, are undoubtedly a great architecture to improve the performance of a software system. Like any other software design technique, they both have their cons. One of the challenges with multithreading/multiprocessing applications is Race condition.

Quick definition:
Reference count is how a program know if an object(i.e variable) is in used or not so that it can free up the memory alocated to it. This is also known as Garbage Collection.
In a language like Python or Java, it's done automatically by the language.

Race condition occurs when two or more threads/processes attempt to modify(increase/decrease) the reference count value of a shared object (basically anything such as variable) simultaneously. This can lead to a bug in the program if it happens.

In an attempt to prevent this kind of scenario, a lock can be added to each shared object. This serves as a guard to such object so that any other thread/process won’t accidentally modify it.

When multiple locks exist, it can lead to a situation where Thread(or Process ) A is waiting(i.e.pause execution) because it needs a shared resource that Thread(or Process) B is holding(locking) and Thread B is as well waiting because it needs a shared resource that Thread A is locking. Technically both Threads or Processes would hang and won’t be able to proceed with their respective execution. This occurrence is termed Dead Lock. It has high potential of decreasing the performance of the program.

Python prevents Dead Lock by introducing Global Interpreter Lock(GIL) which add only one Lock to the interpreter.
This makes multi-threaded design in multi-core CPUs to only run just only one thread at a time.

It's worth noting that GIL exists only in CPython interpreter. That means Pypy, Jython, and other Python interpreters don't have GIL limitations.

Enjoyed this article?

Feel free to explore my video collection on YouTube for more educational content. Don’t forget to subscribe to the channel to stay updated with future releases.

#python #softwaredevelopment #architecture #softwaredesign #performance

Ridwan Yusuf
Ridwan Yusuf

Written by Ridwan Yusuf

RidwanRay.com -I provide engineers with actionable tips and guides for immediate use

No responses yet