So .. let's assume this scenario ..
Code:
Main ()
{
crete mutex;
create N threads;
}
Thread_xx ()
{
.. things ..
// critical section code touching vars
WaitForSingleObject ( mutex, INFINITE );
.. do critical stuff ..
ReleaseMutex ( mutex)
.. etc ..
}
And let's assume this very similar scenario ..
Code:
Main ()
{
InitialiseCriticalSection ( §ion, .. );
create N threads;
}
Thread_xx ()
{
.. things ..
// critical section code touching vars
EnterCriticalSection( §ion );
.. do critical stuff ..
ReleaseCriticalSection( §ion );
.. etc ..
}
The question is "which one is better and why ?".
According to MS docs .. the critical section should be "more efficient" than the mutex, plus it has the bonus
you can use the "spin count" to avoid "using the semaphore and give it a bit more tries" that appearently can be
useful in true multi-processor machines saving CPU time.
The only difference that seems to be ( in such use ) between Mutex and Critical Section is that the Mutex can be
used among different
processes ( not threads ) while the Critical Section not but "by nose" looks like the
Critical Section is more efficient way to do that kind of stuff.
Any thoughts ?
Ah the code in those sections is going to be very small and mostly work with an array of pointers to sort out some
things.
It's also going to be a "once in a while event" not a "pretty frequent" one.
I wonder, but let's forget this for now, how in a real multi-core/multi-processor system nowadays the problem of
cache coherency is solved, I suppose that to add
volatile qualifier to some vars it's probably not enough, I am not
very expert ( yet ) but I suppose that "multi core" CPUs does not have this problem (?).