← Back to Gists

RAII lock guard

📝 C++
joyce_bush
joyce_bush · Level 2 ·

Automatically acquires a mutex lock on construction and releases it on destruction, ensuring exception-safe synchronization.

C++
#include <mutex> std::mutex mtx;
int shareddata = 0; void safeincrement() { std::lockguard<std::mutex> lock(mtx); ++shareddata;
}

Comments

No comments yet. Start the discussion.