Namespaces
Variants

std::unique_lock<Mutex>:: unique_lock

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
unique_lock ( ) noexcept ;
(1) (seit C++11)
unique_lock ( unique_lock && other ) noexcept ;
(2) (seit C++11)
explicit unique_lock ( mutex_type & m ) ;
(3) (seit C++11)
unique_lock ( mutex_type & m, std:: defer_lock_t t ) noexcept ;
(4) (seit C++11)
unique_lock ( mutex_type & m, std:: try_to_lock_t t ) ;
(5) (seit C++11)
unique_lock ( mutex_type & m, std:: adopt_lock_t t ) ;
(6) (seit C++11)
template < class Rep, class Period >

unique_lock ( mutex_type & m,

const std:: chrono :: duration < Rep, Period > & timeout_duration ) ;
(7) (seit C++11)
template < class Clock, class Duration >

unique_lock ( mutex_type & m,

const std:: chrono :: time_point < Clock, Duration > & timeout_time ) ;
(8) (seit C++11)

Konstruiert einen unique_lock , optional mit Sperren des bereitgestellten Mutex.

1) Konstruiert einen unique_lock ohne zugehöriges Mutex.
2) Move-Konstruktor. Initialisiert den unique_lock mit dem Inhalt von other . Lässt other ohne zugehöriges Mutex zurück.
3-8) Konstruiert einen unique_lock mit m als zugehörigem Mutex. Zusätzlich:
3) Sperrt den zugehörigen Mutex durch Aufruf von m. lock ( ) .
4) Sperrt den zugehörigen Mutex nicht.
5) Versucht, den zugehörigen Mutex ohne Blockierung zu sperren, indem m. try_lock ( ) aufgerufen wird. Das Verhalten ist undefiniert, wenn Mutex nicht Lockable erfüllt.
6) Nimmt an, dass der aufrufende Thread bereits eine nicht-geteilte Sperre (d.h. eine durch lock , try_lock , try_lock_for , oder try_lock_until erworbene Sperre) auf m hält. Das Verhalten ist undefiniert, wenn dies nicht der Fall ist.
7) Versucht, den zugehörigen Mutex durch Aufruf von m. try_lock_for ( timeout_duration ) zu sperren. Blockiert bis zur angegebenen timeout_duration verstrichen ist oder die Sperre erworben wurde, je nachdem, was zuerst eintritt. Kann länger blockieren als timeout_duration . Das Verhalten ist undefiniert, wenn Mutex nicht TimedLockable erfüllt.
8) Versucht, den zugehörigen Mutex durch Aufruf von m. try_lock_until ( timeout_time ) zu sperren. Blockiert bis zum Erreichen des angegebenen timeout_time oder bis die Sperre erlangt wird, je nachdem, was zuerst eintritt. Kann länger blockieren als bis timeout_time erreicht wurde. Das Verhalten ist undefiniert, wenn Mutex nicht TimedLockable erfüllt.

Parameter

other - ein weiterer unique_lock zur Initialisierung des Zustands
m - Mutex, der mit der Sperre assoziiert und optional übernommen werden soll
t - Tag-Parameter zur Auswahl von Konstruktoren mit verschiedenen Sperrstrategien
timeout_duration - maximale Dauer für die Blockierung
timeout_time - maximaler Zeitpunkt, bis zu dem blockiert wird

Beispiel

#include <iostream>
#include <mutex>
#include <thread>
#include <utility>
#include <vector>
std::mutex m_a, m_b, m_c;
int a, b, c = 1;
void update()
{
    {   // Hinweis: std::lock_guard oder atomic<int> kann stattdessen verwendet werden
        std::unique_lock<std::mutex> lk(m_a);
        ++a;
    }
    {   // Hinweis: siehe std::lock und std::scoped_lock für Details und Alternativen
        std::unique_lock<std::mutex> lk_b(m_b, std::defer_lock);
        std::unique_lock<std::mutex> lk_c(m_c, std::defer_lock);
        std::lock(lk_b, lk_c);
        b = std::exchange(c, b + c);
    }
}
int main()
{
    std::vector<std::thread> threads;
    for (unsigned i = 0; i < 12; ++i)
        threads.emplace_back(update);
    for (auto& i : threads)
        i.join();
    std::cout << a << ". und " << a + 1 << ". Fibonacci-Zahlen: "
              << b << " und " << c << '\n';
}

Ausgabe:

12. und 13. Fibonacci-Zahlen: 144 und 233