This code is thread safe... as all writes and reads to and from primitive types like bools are atomic. Although, you do have two checks on the
bool happening here... in the while declaration, and in the body of the while statement.
There are other issues that can crop up concerning compiler optimizations, and possibly optimizing your code in a way that renders any changes to the
boolean invisible to the checking thread.
Take a look at the volatile keyword in C# for more information on compiler optimization, and how to avoid it on specific variables.