Namespaces
Variants
Views
Actions

std::unique_lock

From cppreference.com
< cpp | thread
Revision as of 12:49, 14 July 2012 by Nate (Talk | contribs)
Defined in header <mutex>
template< class Mutex >
class unique_lock;
(since C++11)

The class unique_lock is a general-purpose mutex ownership wrapper allowing deferred locking, timed locking, recursive locking, transfer of lock ownership, and use with condition variables.

The unique_lock class is non-copyable, but it is movable. The supplied Mutex type shall implement the BasicLockable concept.

Contents

Member types

Type Definition
mutex_type Mutex

Member functions

constructs a unique_lock, optionally locking the supplied mutex
(public member function) [edit]
unlocks the associated mutex, if owned
(public member function) [edit]
unlocks the mutex, if owned, and acquires ownership of another
(public member function) [edit]
Locking
locks the associated mutex
(public member function) [edit]
tries to lock the associated mutex, returns if the mutex is not available
(public member function) [edit]
attempts to lock the associated TimedLockable mutex, returns if the mutex has been unavailable for the specified time duration
(public member function) [edit]
tries to lock the associated TimedLockable mutex, returns if the mutex has been unavailable until specified time point has been reached
(public member function) [edit]
unlocks the associated mutex
(public member function) [edit]
Modifiers
swaps state with another std::unique_lock
(public member function) [edit]
disassociates the associated mutex without unlocking it
(public member function) [edit]
Observers
returns a pointer to the associated mutex
(public member function) [edit]
tests whether the lock owns its associated mutex
(public member function) [edit]
tests whether the lock owns its associated mutex
(public member function) [edit]

Non-member functions

specialization of std::swap for unique_lock
(function template) [edit]

Example

#include <mutex>
#include <thread>
#include <chrono>
 
struct bank_account {
    explicit bank_account(int balance) : balance(balance) {}
    int balance;
    std::mutex m;
};
 
void slow_log(bank_account &from, bank_account &to, int amount)
{
    // simulate a very slow logging operation
    std::this_thread::sleep_for(std::chrono::seconds(10));
}
 
void transfer(bank_account &from, bank_account &to, int amount)
{
    // don't actually take the locks yet
    std::unique_lock lock1(from.m, std::defer_lock);
    std::unique_lock lock2(to.m, std::defer_lock);
 
    // lock both unique_locks without deadlock
    std::lock(lock1, lock2);
 
    from.balance -= amount;
    to.balance += amount;
 
    // unlock the locks so the slow logging calls won't block eachother
    int from_copy = from.balance;
    int to_copy = to.balance;
 
    lock1.unlock();
    lock2.unlock();
 
    slow_log(from_copy, to_copy, amount);
}
 
int main()
{
    bank_account my_account(100);
    bank_account your_account(50);
 
    std::thread t1(transfer, my_account, your_account, 10);
    std::thread t2(transfer, your_account, my_account, 5);
 
    t1.join();
    t2.join();
}