C++ 23 · Classes, memory and templates

Lock a mutex safely

RAII locking releases the mutex even if an exception is thrown.

std::mutex m;
{
    std::lock_guard<std::mutex> lock(m);
    ++counter;
}

Official C++ 23 documentation

More classes, memory and templates commands

All classes, memory and templates commands · C++ 23 cheatsheet

Was this page helpful?