Is there anything faster than that ?
If busy-wait is appropriate in your case, you can try to use spinlock implemented on top of boost::atomic_flag
.
SO: When should one use a spinlock instead of mutex?
class spinlock
{
atomic_flag flag = ATOMIC_FLAG_INIT;
public:
void lock()
{
while(flag.test_and_set(memory_order_acquire));
}
void unlock()
{
flag.clear(memory_order_release);
}
};
void foo()
{
static spinlock lock;
lock_guard<spinlock> guard(lock);
// do job
}
boost::lock_guard<boost::recursive_mutex> lock(m_guard);
it seems that everytime methodA() would be called lock would be created on the stack.
It is not lock, it is just RAII wrapper, which locks mutex in constructor and releases it in destructor. It is OK, to create lock_guard on stack.