lock on bool when access via multiple threads

Currently I check on a bool to determine whether one of the threads should stop work. The bool is set from the other thread. Do I need to lock around the bool? I presumed I would, but collegues believe I may not have to? Another collegue mentioned setting the bool to volatile, so it's not cached? I appreciate if anyone could suggest the correct action.

If now the first thread sets the bool to true, the second may still read a false if the read operation was nearly same time (or same time at another core). But any little time later, it correctly would read the true. So, the only reason for a lock could be if the second must recognize the setting to true immediately, e. g. in case the bool is used for synchronization reasons itself. For these cases a unlocked bool is not safe, you would need amutex, critical section or at least an atomic increment, e. g. by calling InterLockedIncrement on a shared long:

volatile long g_stop = 0;
...

while (InterLockedIncrement(&g_stop) > 1)
{
// coming here a second thread has incremented the g_stop nearly same time
InterLockedDecrement(&g_stop);
Sleep(1);
}
// coming here it is safe now
....

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into.
One of these problems is selecting and deleti…

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ...
Some objects defined and used in this source include:
…