Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent to boost::interprocess::null_mutex in C++11 (e.g. std::null_mutex)?

Tags:

c++

c++11

mutex

I find the ability to drop in a null_mutex (currently boost::interprocess::null_mutex) very useful when I don't want the synchronization overhead in some cases and a real mutex in others.

I am trying to use the new c++11 mutex classes, but I see no equivalent for null_mutex - which leaves me puzzled..

Yes I know it's trivial to implement (or I can continue to use boost, but where possible I'm trying to stick the standard and seems like a small omission?)

like image 654
Nim Avatar asked Nov 16 '12 09:11

Nim


1 Answers

You can make this fairly trivially, by creating a 'null' implementation of the Lockable concept:

struct null_mutex
{
     void lock() {}
     void unlock() noexcept {}
     bool try_lock() { return true; }
};

This would work with std::lock_guard:

null_mutex mux;
std::lock_guard<null_mutex> guard(mux);
like image 198
sehe Avatar answered Oct 15 '22 06:10

sehe