I wrote a simple code where I take a unique_lock and unlock the mutex instead of calling unlock on the lock itself. When the first thread enters the critical section and calls my_mutex.unlock(), many other threads enter the critical section together.
std::mutex my_mutex;
void sample() {
std::unique_lock<std::mutex> lock(my_mutex);
// Critical section
my_mutex.unlock();
}
Why is this happening? Is it wrong to call unlock on a mutex held by a unique_lock? Thanks!