views:

35

answers:

1

Why would one need a mutex object where the Acquire and release methods just return 0?

I am studying the ACE framework and it has a Null_Mutex class, and I was wondering how it would come to use.

class Null_Mutex
{
public:
Null_Mutex (void) {}
˜Null_Mutex (void) {}
int remove (void) { return 0; }
int acquire (void) const { return 0; }
int try_acquire (void) const { return 0; }
int release (void) const { return 0; }
};
+3  A: 

It's null object pattern: you can pass it to code requiring mutex when you don't need actual mutex logic.

ymv
Can you give an example where that would be the case?
Tony
+1. @Tony: For example, you might want to run your code on one thread only, then using real mutexes would not be needed, but would introduce significant overhead. With null mutex you don't need to rewrite all code - you just pass a null mutex, the code is happy to "use" it and you don't have the overhead.
sharptooth