Post by Allan M. B » Wed, 12 Apr 2006 20:18:40

I have been doing some multithreaded applications for a while now but am
still a beginner by any means. I was wanting to know if there are any
differences between:

static lock
critical section

or are these just implementation terminology for the same process?



Post by Dave Buten » Wed, 12 Apr 2006 21:54:12

That depends very much on context. There are certainly contexts where
all could be considered "equivalent", theoretically. In practice, when
it comes down to writing code, they mean very different things.

(In theory, there's no difference between theory and practice...)

Or to put it another way... "what's your question?" ;-)



Post by Doug » Sat, 15 Apr 2006 08:38:01

Hiya matey. One last beer before I hit the sack for the long day
tomorrow. Here's my tuppence. From a posixy point of view.
Rearranged the order to make it easier to explain.

static lock:
Not sure what you mean by the use of the word static here. I *think*
you just mean mutex, see below.

This is a mechanism to provide 'mutual exclusion'. Often just called a
'lock'. If you acquire a mutex, you are guaranteed to be the only
person holding it. Typically you'll block until you can acquire it,
though there are various models. You would normally use a mutex to
protect some data:

int thingsToDo = 2; thingsDone = 0; // Must be kept consistent or
nuclear missiles will be launched

} // avoid WWIII

Anyone reading either var would have to grab the same aMutex.

The word has become diluted, though. These days people say 'mutex' to
mean some kind of lock in general, be it a traditional mutex, a rw
lock, a spinlock, a semaphore, etc...

This is usually like a mutex but with a count. You can think of it
like a pub on a Friday night. The first x people get in. After that,
it's a one-in, one-out policy. You normally decide x when you
create/init the semaphore.
Some models allow threads that don't own the sempahore to 'release' it.
(E.g. the bouncer spots a drunk stumbling out the fire exit, so lets
someone else in the front door.) Some people call these 'signal
sempahores' - they achieve roughly the same effect as mutex-condvar

critical section:
I think there are two main definitions for this. The first is a
finite, single piece of code where someone (the compiler or the
hardware) guarantees that at most one thread will run in it
Today's definition amongst most I know is the Microsoft version - it's
basically a mutex.

The implementation and perf of each mechanism is very different,

You might also want to read up on:
- spinlocks
- recursive locks
- rw locks

There's also lots of good lock-free stuff in this group.

Don't know why i did this by ng, will speak to you tues.