In Java, we can't share a local variable between threads unless the final keyword is added. But in C#, it's allowed to write like this:
void DoSomeJob() {
bool isDone = false;
new Thread( ()=> {
// Do some background job
isDone = true;
}).Start();
while (isDone == false) {
// Do some foreground job
}
}
Actually, this worked in my simple tests.
-As you see, no Thread.Sleep() call or anything similar which causes reloading.
-Ran in both debug and release mode.
If I had to share a variable between threads (without any locks) like this, I would define it as a static variable with the volatile keyword to prevent running into an infinite loop. So I wonder if just simply reading a local variable like above will always work.
Btw, this is just a question out of curiosity, but not about writing better-multithreaded code.