Lets say we have this code:
bool KeepGoing = true;
DataInThread = new Thread(new ThreadStart(DataInThreadMethod));
DataInThread.Start();
//bla bla time goes on
KeepGoing = false;
private void DataInThreadMethod()
{
while (KeepGoing)
{
//Do stuff
}
}
}
Now the idea is that using the boolean is a safe way to terminate the thread however because that boolean exists on the calling thread does that cause any issue?
That boolean is only used on the calling thread to stop the thread so its not like its being used elsewhere
You need to clarify exactly what / where
KeepGoingis, but no; it is not safe. It can be held in a register on x86 (but this is unlikely in most non-trivial examples). You need to make itvolatile, or to synchronize (locketc) access to it. For an example, see here.