I have a macro that either declares a RAII mutex when I'm compiling a program normally, or skips it for efficiency when I'm compiling in a single-threaded mode (where I set SINGLE_THREADED=1
). Obviously I would prefer to not use macros as much as possible. I'm using C++20 (g++) - is there a way to refactor a macro like this to use constexpr
or a template instead of #define
?
My current implementation is:
#ifdef SINGLE_THREADED
#define READ_LOCK(name, mutex) ({})
#else
#define READ_LOCK(name, mutex) folly::SharedMutex::ReadHolder name(mutex);
#endif
I use it in my code like:
void foo() {
READ_LOCK(lg, my_mutex);
// ....
}