# Thread safety now and in the future (no-gil)

**URL:** <https://discuss.python.org/t/thread-safety-now-and-in-the-future-no-gil/104297>\
**Category:** Threading\
**Created:** [October 10, 2025, 6:12pm UTC](https://discuss.python.org/t/thread-safety-now-and-in-the-future-no-gil/104297 "2025-10-10T18:12:12Z")\
**Posts on this page:** 1\
**Showing post:** 73

<div class="post-metadata">

**Author:** ![dpdani](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dpdani/32/14107_2.png) [@dpdani](https://discuss.python.org/u/dpdani)\
**Post date:** [November 5, 2025, 6:36pm UTC](https://discuss.python.org/t/thread-safety-now-and-in-the-future-no-gil/104297/73 "2025-11-05T18:36:51Z")

</div>

I have said something wrong in my previous post. I think the correction is very relevant to this discussion, so I’ll try to explain my mistake as clearly as I can.

### What was wrong

I said that in this example program, thread 2 will see the write `x = 2` in all memory models:

> [@romank0](#):
>
> Let’s consider a simplier case like this:
> 
> 1. x = 1
> 2. thread 1 sets x= 2
> 3. thread 2 waits in a loop until x = 2

That is not true. In fact, a very similar example is given in Java’s memory model specification, stating that the write may never be read by thread 2. Let me report the Java code from the example in the specification (section 3.1):

```java
class LoopThatMayNeverEnd {
  boolean done = false;

  void work() {
    while (!done) {
      // do work
    }
  }

  void stopWork() {
    done = true;
  }
}

```

According to the Java MM, and assuming that one thread is executing the `work()` method and a different thread eventually calls the `stopWork()` method, it is entirely possible that the loop never ends. (Note that the MM does not specify whether it will end.) I think it’s worth investigating the reason why the Java specification allows for this.

The reason why the `work()` thread may never exit the loop is that a Java compiler can optimize the loop to completely eliminate the read on the variable `done`. After all, the thread that is performing the loop never writes into the variable, so why bother checking it? The Java compiler simply doesn’t know that `done` is a shared variable.

### The significance for Python

At this point, it’s important to mention that CPython (the implementation of the language that people commonly use) does not currently host a compiler that does this kind of optimization. Therefore, this sort of problem never arises in Python code. Let’s look at the Python version of the example above:

```python
done = False

def work():
    while not done:
        # do work

def stopWork():
    done = True

```

Even though the current JIT compiler will not eliminate the `while not done` check, it is not inconceivable that one day it might, for the same reasons that Java compilers do this kind of optimizations. Hence, if we want to seriously address the main problem of this thread, we must answer two questions:

1. is this Python code safe now? Yes.
2. will this Python code be safe in the future? Maybe.

If current proposals like Shannon’s [PEP 805](https://discuss.python.org/t/pre-pep-safe-parallel-python/103636) do go forward, then I don’t see why the above code should become unsafe in the future. (And it’s very desirable that it stays safe, so I reckon there’s a very good chance it will.) Nevertheless, as it stands we should face the fact that we’re not promising anything here.

I think that the answer to the first question is not at all to be dismissed: in my view, ongoing efforts to improve Python’s documentation should include an explanation of the fact that this example is safe in Python, differently from other languages. Notably including Java, a very popular high-level language. People with those background have every reason to be concerned about these problems, and we shouldn’t make the mistake of dismissing their concerns; something which was done not long ago by yours truly.

---

_[View the full topic](https://discuss.python.org/t/thread-safety-now-and-in-the-future-no-gil/104297)._
