# What is Thread Safety as applied to Python?

**URL:** <https://discuss.python.org/t/what-is-thread-safety-as-applied-to-python/13685>\
**Category:** Python Help\
**Created:** [February 13, 2022, 7:14am UTC](https://discuss.python.org/t/what-is-thread-safety-as-applied-to-python/13685 "2022-02-13T07:14:06Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [February 13, 2022, 1:22pm UTC](https://discuss.python.org/t/what-is-thread-safety-as-applied-to-python/13685/4 "2022-02-13T13:22:42Z")

</div>

Removing the GIL does not make functions thread-safe.

Thread-safety relates to this question:

If you have a data structure (say, an object, or a list) that is being modified in one thread, can another thread come along and modify it right in the middle of that calculation?

Let’s say you have a global variable x which equals 0, and two different threads attempt to increment it using this:

```
x += 1

```

If incrementing is thread-safe, then you are guaranteed that the result can only be x=2 after the two threads have incremented it. Thread safety guarantees that the only way those two threads can increment x looks like this:

1. thread A locks x, so nothing else can touch it;
2. thread A reads the value of x, and gets 0;
3. thread A adds 1 to the value, giving 1;
4. thread A writes 1 to x;
5. thread A unlocks x, so other threads can use it;
6. thread B locks x;
7. thread B reads the value of x, and gets 1;
8. thread B adds 1 to the value, giving 2;
9. thread B writes 2 to x;
10. and thread B unlocks x.

Of course the locks and unlocks aren’t free, that takes time, and it also means that thread B has to wait for thread A to finish, which slows it down. So thread-safety isn’t cheap.

But without thread-safety, you can have this:

1. thread A reads the value of x, and gets 0;
2. thread B reads the value of x, and gets 0;
3. thread A adds 1 to the value, giving 1;
4. thread A writes 1 to x;
5. thread B adds 1 to the value, giving 1;
6. thread B writes 1 to x.

So now you have a bug where two threads have both tried to increment x, both think they have done so, but x only equals 1 instead of 2.

Welcome to the Quadrant Of Hell:

[[Album] Quadrant of Hell ![](https://us1.discourse-cdn.com/flex002/uploads/python1/original/2X/3/316a73b41661d447bf3c5705affaeed9e9b53182.jpeg "Quadrant of Hell") ](https://imgur.com/a/JWkjMMn)

---

_[View the full topic](https://discuss.python.org/t/what-is-thread-safety-as-applied-to-python/13685)._
