Re: EnterCriticalSection

Tech-Archive recommends: Fix windows errors by optimizing your registry



"Elcaro Nosille" <Elcaro.Nosille@xxxxxxxxxxxxxx> wrote in message news:46dc59d2$0$7690$9b4e6d93@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
EnterCriticalSection section is extremely fast when there's no
contention with another thread holding this critical section.

[...]

The is a false assertion. Describing the operation as "extremely fast" is really misleading to say the least. You probably don't know that acquiring and releasing an uncontended lock involves the use of 2 interlocked RMW instructions and 2 memory barriers, one of which is very expensive. Example using SPARC membar instruction:

lock() {
Interlocked RMW
membar #StoreLoad | #StoreStore // expensive!
}


unlock() {
membar #LoadStore | #StoreStore
Interlocked RMW
}


On Intel architectures, the #StoreLoad | #StoreStore memory barrier is handled by the LOCK prefix which is not a lightweight operation to say the least:

http://groups.google.com/group/comp.programming.threads/msg/aeb7e857ef440520

http://groups.google.com/group/comp.programming.threads/msg/fdc665e616176dce


FWIW, here is a paper that details the Intel Memory Model:

http://developer.intel.com/products/processor/manuals/318147.pdf



.