std::mutex::unlock

From cppreference.com

void unlock();
(since C++11)

Unlocks the mutex.

The mutex must be unlocked by all threads that have successfully locked it before being destroyed. Otherwise, the behavior is undefined.

Contents

[edit] Parameters

(none)

[edit] Return value

(none)

[edit] Exceptions

(none)

[edit] Example

This example shows lock, try_lock and unlock in action

#include <iostream>
#include <mutex>
 
int main()
{
    std::mutex test;
    if (test.try_lock()==true)
        std::cout << "lock acquired" << std::endl;
    else
        std::cout << "lock not acquired" << std::endl;
    test.unlock();	//now unlock the mutex
    test.lock();	//to lock it again
    if (test.try_lock())  //true can be left out
        std::cout << "lock acquired" << std::endl;
    else
        std::cout << "lock not acquired" << std::endl;
    test.lock(); //and now the finale (a block)
}

Output:

lock acquired
lock not acquired
(program hangs)

[edit] See also

locks the mutex, blocks if the mutex is not available
(public member function)
tries to lock the mutex, returns if the mutex is not available
(public member function)