Calling unlock() by hand fails the first time a return or an exception skips it: the mutex stays locked and the next thread to lock it waits forever. Lock guards apply RAII to mutexes.
std::lock_guard<std::mutex> lock(m); locks in its constructor and unlocks in its destructor, whichever way the scope ends. Give it a name: std::lock_guard<std::mutex>{m}; is a temporary that unlocks at the end of that same line.
std::unique_lock<std::mutex> does the same, and can also unlock() early, lock again, or be moved. Use it to release the lock before slow work, and with condition variables (later in this unit).
std::scoped_lock lock(a, b); locks several mutexes at once, in a way that cannot deadlock against another thread locking the same ones in a different order.
Keep the locked section short: copy what you need, unlock, then do the slow part.
Threads here are simulated. Your program runs on one real thread while a scheduler switches between your threads at shared reads and writes, locks, atomic operations, waits, yields and sleeps, and every test runs under 12 different schedules. The order things can happen in, races at those points and deadlocks are faithful. Real parallel speed-up and weak memory-order effects are not.
A guard unlocks on every path, and unique_lock can unlock early.