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;
}
More classes, memory and templates commands
- Define a class
- Use polymorphism
- Own an object with unique_ptr
- Share ownership with shared_ptr
- Move instead of copy
- Write a function template
All classes, memory and templates commands · C++ 23 cheatsheet