2

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; }
};
Henrik
  • 23,186
  • 6
  • 42
  • 92
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415

1 Answers1

4

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

ymv
  • 2,123
  • 13
  • 21
  • Can you give an example where that would be the case? – Tony The Lion Sep 09 '10 at 07:53
  • 2
    +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 Sep 09 '10 at 09:56
  • Except for the fact that abstracting out your mutexes like this and calling all the methods through indirect calls (function pointers) costs a lot more than a direct call to the mutex lock and unlock functions... – R.. GitHub STOP HELPING ICE Feb 01 '12 at 02:29