Re: volatile char
- From: "Igor Tandetnik" <itandetnik@xxxxxxxx>
- Date: Tue, 5 Jul 2005 11:48:45 -0400
"Dan Baker" <dbmail> wrote in message
news:utZQvRXgFHA.1048@xxxxxxxxxxxxxxxxxxxx
> Useless? What about the following case? There are several worker
> threads, working away. They don't have much interaction with the
> main thread. But, when there are times (like application shutdown)
> when the main thread simply wants all worker threads to stop. I have
> worker thread code like the following:
>
> class CMyThread
> {
> ...
> volatile BOOL m_bRequestToStop; // Set to TRUE by master thread
> to abort this worker thread
> void run();
> }
>
> void CMyThread::run
> {
> while (!m_bRequestToStop)
> {
> ... perform calculations here ...
> }
> }
"volatile" here just makes your program more likely to appear to work.
It exploits a particular feature of x86 CPU family - strong cache
coherence. As soon as one CPU writes to a memory location, the caches of
all the other CPUs are forcibly updated with the new value. This is one
of the reasons x86 does not scale beyong 4-way SMP systems - cache sync
traffic grows quadratically with the number of CPUs.
Many modern CPUs, some of which can run Windows, exhibit weak cache
coherency. Under this model, even after one CPU writes to a memory
location, the other CPUs may observe old values when reading this memory
location (if the location happens to be in this CPU's cache) for an
indeterminate (though finite) amount of time. A special machine
instruction called a "memory barrier" forces the CPU to synchronize its
cache contents with the main memory.
So, on such a system, once the main thread sets m_bRequestToStop to
true, the other threads may continue observing the value of false for
some time (though not indefinitely, so they will break out of the loop
sooner or later). Well, I guess this is not the end of the world.
Still, the correct way to implement this is one of the following:
1. Use InterlockedIncrement or InterlockedExchange to bump the flag from
0 to 1 in the main thread; use InterlockedCompareExchange in the threads
to check the value of the flag
2. Set and read the flag under a critical section
3. Use an event (see CreateEvent) instead of a boolean flag.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
.
- Follow-Ups:
- Re: volatile char
- From: Lawrence Groves
- Re: volatile char
- References:
- volatile char
- From: boki
- Re: volatile char
- From: David Lowndes
- Re: volatile char
- From: Lau Lei Cheong
- Re: volatile char
- From: Igor Tandetnik
- Re: volatile char
- From: Dan Baker
- volatile char
- Prev by Date: Re: Template & Source organization
- Next by Date: Re: volatile char
- Previous by thread: Re: volatile char
- Next by thread: Re: volatile char
- Index(es):
Relevant Pages
|