Re: Heap memory available (W32 console app in Visual C++)
CriCri <bitwyse@leTIRETmaquis.net> wrote:
Hello Doug
Doug Harrison [MVP] a ?crit :
That's the desired result, but it depends completely on the threads
all acquiring the same mutex before entering the critical section.
Now I just don't understand that statement _at all_! - it's a
contradiction in terms.
A mutex is like a token in a token ring: once someone has got it
nobody else can have it.
Has Microsoft redefined what is a 'mutex' too? (is that what your
'pet peeve' is?)
Consider:
int counter = 0;
Mutex mutex;
// thread 1
AquireMutex(&mutex);
++counter;
ReleaseMutex(&mutex);
// thread 2
AquireMutex(&mutex);
++counter;
ReleaseMutex(&mutex);
// tread 3
++counter;
Thread 1 and thread 2 cannot modify counter simultaneously: as you
noted, only one thread at a time can hold a mutex. But thread 3 isn't so
restricted: since it never tries to acquire the mutex, nothing prevents
it from manipulating counter at the same time as thread 1 or thread 2.
When you try to protect shared data with a mutex, all threads accessing
this shared data must cooperate, in that all of them must acquire the
mutex before touching said data. If even one doesn't, you get race
conditions.
"A critical section consists of a block of instructions delimited by
starting and ending system calls (mutex acquisition and release).
The result of executing that block MUST be identical in every way
concerning the process and all its associated data to that which would
have been obtained if it had been the only one executing in the
system."
What precisely is "the process' associated data"? Is the total amount of
free memory available to a system as a whole considered "associated
data"? What about the state of the hard drive (in other words, can an
I/O operation proceed in another process while your process is in
critical secion)? The availability of IP ports, serial ports, and other
system-wide shared resources?
If the answer is "yes" - that is, the whole system must freeze in its
tracks while one thread is in a critical section - then no such facility
is available to a user-mode Windows application.
--
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