Search This Blog

Tuesday, May 31, 2005

(Fast) Mutexes - Implementation Rationale

Both Windows and POSIX support mutexes natively, but I chose not to use either, for different reasons.

On Windows, a mutex is a kernel mode object. That is, it's a slow mutex. This is much too slow for us, and almost always a fast mutex will be sufficience, and much, much faster. Windows also offers a fast mutex called a critical section. I chose not to use this because it does not offer one of the features we desire: the ability to try-enter the mutex (this feature was introduced with NT 4.0, and is not available on Windows 9x systems). As well, support for spinning is not available on Windows 95 or NT 4.0 pre-service pack 3.

POSIX mutexes, on the other hand, are fast. But they, too, lack features that we desire. While POSIX mutexes support try-enter, they do not support spinning. As mutexes are perhaps the place where spinning is most beneficial (as often a thread will only enter the mutex for long enough to update variable or two), this is undesirable for us.

So, we're going to write our own fast mutex.

1 comment:

Anonymous said...

POSIX does support spinning - pthread_spin_lock().