Without being volatile and without any kind of synchronization, the code you showed is not thread safe. One thread can change the value of a variable, and another thread will never see it, looping forever. If you synchronize all access to it using a shared lock, this is thread safe.
Now volatility is a complex topic - I thought I understood it until recently, but it doesnโt mean that I thought it did , However, Iโm sure that just changing the volatile variable will do what you need.
For something more complicated, I usually use full synchronization: take out the lock every time you access shared data.
source share