Re: Threads - When?
Dilip wrote:
James Kanze wrote:
Le Chaud Lapin wrote:
Also, to remain honest, I did not do critical sections, which means I
use Mutex's exclusively for locking, with performance hit, but that
will fixed.
I presume here that you are confusing the name of the system
request with the actual functionality it provides. I know of no
system today which provides a pure critical section; what
Windows calls a critical section is in fact a mutex.
No. The OP is right in this regard. Critical Sections are a
lightweight mechanism to synchronize access between 2 threads *within*
the same process.
They implement what has commonly been called a mutex in the
threading community. Not a critical section. The two are
fundamentally different (although you can use a mutex to
implement critical sections, and vice versa). Depending on the
implementation of threads, in case of contention, you need a
system call to suspend the thread.
Grabbing a critical section is probably an atomic
test-and-set instruction.
Acquiring a mutex which is currently free is almost certainly a
single atomic instruction (with the necessary fences and/or
memory barriers, of course) in any modern OS. But that's
irrelevant. Mutexes and critical sections do different things:
a mutex protects data, where as a critical section protects a
part of the code. The semantics of a CriticalSection, under
Windows, are exactly those of a mutex, and quite different from
those of a critical section. (I'm most familiar with critical
sections from the work of Per Brinch-Hansen, but I think Hoare
was involved with their definition as well.)
Mutexes on the other hand, have an
independant existence under windows. However critical sections do have
to drop down to kernel mode if a lock is under contention. If you are
interested you can take a look at this link:
http://msdn.microsoft.com/msdnmag/issues/03/12/CriticalSections/default.aspx
It delves into great depths.
Don't get Microsoft's names for their API confused with the
underlying concepts, which have been around for a lot longer
than Windows. (Per Brinch-Hansen's monitor processes date from
the 1970's. And a mutex is a special case of a semaphore, which
was invented by Dijkstra, and is an even older concept.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]