Namespaces
Variants
Views
Actions

std::timed_mutex::lock

From cppreference.com
void lock();
(since C++11)

Locks the mutex. Blocks until the lock is acquired.

Contents

[edit] Parameters

(none)

[edit] Return value

(none)

[edit] Exceptions

[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

tries to lock the mutex, returns if the mutex is not available
(public member function) [edit]
tries to lock the mutex, returns if the mutex has been
unavailable for the specified timeout duration
(public member function) [edit]
tries to lock the mutex, returns if the mutex has been
unavailable until specified time point has been reached
(public member function) [edit]
unlocks the mutex
(public member function) [edit]