다음을 통해 공유


unique_lock 클래스

mutex의 잠금 및 잠금 해제를 관리하는 개체를 만들기 위해 인스턴스화할 수 있는 템플릿을 나타냅니다.

구문

template <class Mutex>
class unique_lock;

설명

템플릿 인수 Mutex뮤텍스 형식 이름을 지정해야 합니다.

내부적으로는 unique_lock이 포인터 및 현재 스레드가 mutex를 소유하는지를 나타내는 bool을 연결된 mutex 개체에 저장합니다.

멤버

공용 Typedefs

이름 설명
mutex_type 템플릿 인수 Mutex에 대한 동의어입니다.

공용 생성자

속성 설명
unique_lock unique_lock 개체를 생성합니다.
~unique_lock 소멸자 unique_lock 개체와 연결된 모든 리소스를 해제합니다.

공용 메서드

이름 설명
lock 스레드가 연결된 mutex의 소유권을 가져올 때까지 호출 스레드를 차단합니다.
mutex 연결된 mutex에 대해 저장된 포인터를 검색합니다.
owns_lock 호출 스레드가 연결된 mutex를 소유하는지를 지정합니다.
release 연결된 mutex 개체에서 unique_lock 개체의 연결을 끊습니다.
swap 연결된 mutex 및 소유권 상태를 지정된 개체의 mutex 및 소유권 상태로 바꿉니다.
try_lock 차단되지 않고 연결된 mutex의 소유권을 가져오려고 시도합니다.
try_lock_for 차단되지 않고 연결된 mutex의 소유권을 가져오려고 시도합니다.
try_lock_until 차단되지 않고 연결된 mutex의 소유권을 가져오려고 시도합니다.
unlock 연결된 mutex의 소유권을 해제합니다.

Public 연산자

이름 설명
operator bool 연결된 mutex의 소유권이 호출 스레드에 있는지를 지정합니다.
operator= 저장된 mutex 포인터 및 연결된 소유권 상태를 지정한 개체에서 복사합니다.

상속 계층 구조

unique_lock

요구 사항

헤더:<뮤텍스>

네임스페이스: std

lock

스레드가 연결된 mutex의 소유권을 가져올 때까지 호출 스레드를 차단합니다.

void lock();

설명

저장된 mutex 포인터가 NULL이면 이 메서드는 오류 코드operation_not_permitted가 있는 system_error throw합니다.

호출 스레드가 연결된 mutex를 이미 소유하고 있으면 이 메서드는 오류 코드가 resource_deadlock_would_occursystem_error를 throw합니다.

그렇지 않으면 이 메서드는 연결된 mutex에 대해 lock을 호출하고 내부 스레드 소유권 플래그를 true로 설정합니다.

mutex

연결된 mutex에 대해 저장된 포인터를 검색합니다.

mutex_type *mutex() const noexcept;

operator bool

연결된 뮤텍스의 소유권이 호출 스레드에 있는지를 지정합니다.

explicit operator bool() noexcept

Return Value

스레드가 뮤텍스를 소유하는 경우 true이고 그렇지 않으면 false입니다.

operator=

저장된 mutex 포인터 및 연결된 소유권 상태를 지정한 개체에서 복사합니다.

unique_lock& operator=(unique_lock&& Other) noexcept;

매개 변수

기타
unique_lock 개체입니다.

Return Value

*this

설명

이 메서드가 mutex에 대해 unlock을 호출하기 전에 호출 스레드가 이전에 연결된 mutex를 소유하고 있었다면 새 값이 할당됩니다.

복사 후 이 메서드는 기타를 기본 생성 상태로 설정합니다.

owns_lock

호출 스레드가 연결된 mutex를 소유하는지를 지정합니다.

bool owns_lock() const noexcept;

Return Value

스레드가 mutex를 소유하는 경우 true이고 그렇지 않으면 false입니다.

릴리스

연결된 mutex 개체에서 unique_lock 개체의 연결을 끊습니다.

mutex_type *release() noexcept;

Return Value

저장된 mutex 포인터의 이전 값입니다.

설명

이 메서드는 저장된 mutex 포인터의 값을 0으로 설정하고 내부 mutex 소유권 플래그를 false로 설정합니다.

swap

연결된 mutex 및 소유권 상태를 지정된 개체의 mutex 및 소유권 상태로 바꿉니다.

void swap(unique_lock& Other) noexcept;

매개 변수

기타
unique_lock 개체입니다.

try_lock

차단되지 않고 연결된 mutex의 소유권을 가져오려고 시도합니다.

bool try_lock() noexcept;

Return Value

메서드가 성공적으로 true의 소유권을 가져오면 mutex이고, 그렇지 않으면 false입니다.

설명

저장된 mutex 포인터가 NULL이면 메서드는 오류 코드operation_not_permitted가 있는 system_error throw합니다.

호출 스레드가 mutex를 이미 소유하고 있으면 이 메서드는 오류 코드가 resource_deadlock_would_occursystem_error를 throw합니다.

try_lock_for

차단되지 않고 연결된 mutex의 소유권을 가져오려고 시도합니다.

template <class Rep, class Period>
bool try_lock_for(
    const chrono::duration<Rep, Period>& Rel_time);

매개 변수

Rel_time
메서드가 mutex의 소유권을 가져오려고 시도하는 최대 시간을 지정하는 chrono::duration 개체입니다.

Return Value

메서드가 성공적으로 true의 소유권을 가져오면 mutex이고, 그렇지 않으면 false입니다.

설명

저장된 mutex 포인터가 NULL이면 메서드는 오류 코드operation_not_permitted가 있는 system_error throw합니다.

호출 스레드가 mutex를 이미 소유하고 있으면 이 메서드는 오류 코드가 resource_deadlock_would_occursystem_error를 throw합니다.

try_lock_until

차단되지 않고 연결된 mutex의 소유권을 가져오려고 시도합니다.

template <class Clock, class Duration>
bool try_lock_until(const chrono::time_point<Clock, Duration>& Abs_time);

bool try_lock_until(const xtime* Abs_time);

매개 변수

Abs_time
임계값을 지정하는 특정 시점으로, 이 시간 경과 후에는 메서드가 더 이상 mutex의 소유권을 가져오려고 시도하지 않습니다.

Return Value

메서드가 성공적으로 true의 소유권을 가져오면 mutex이고, 그렇지 않으면 false입니다.

설명

저장된 mutex 포인터가 NULL이면 메서드는 오류 코드operation_not_permitted가 있는 system_error throw합니다.

호출 스레드가 mutex를 이미 소유하고 있으면 이 메서드는 오류 코드가 resource_deadlock_would_occursystem_error를 throw합니다.

unique_lock 생성자

unique_lock 개체를 생성합니다.

unique_lock() noexcept;
unique_lock(unique_lock&& Other) noexcept;
explicit unique_lock(mutex_type& Mtx);

unique_lock(mutex_type& Mtx, adopt_lock_t Adopt);

unique_lock(mutex_type& Mtx, defer_lock_t Defer) noexcept;
unique_lock(mutex_type& Mtx, try_to_lock_t Try);

template <class Rep, class Period>
unique_lock(mutex_type& Mtx,
    const chrono::duration<Rep, Period>
Rel_time);

template <class Clock, class Duration>
unique_lock(mutex_type& Mtx,
    const chrono::time_point<Clock, Duration>
Abs_time);

unique_lock(mutex_type& Mtx,
    const xtime* Abs_time) noexcept;

매개 변수

Mtx
뮤텍스 형식 개체입니다.

Rel_time
메서드가 mutex의 소유권을 가져오려고 시도하는 최대 시간을 지정하는 chrono::duration 개체입니다.

Abs_time
임계값을 지정하는 특정 시점으로, 이 시간 경과 후에는 메서드가 더 이상 mutex의 소유권을 가져오려고 시도하지 않습니다.

기타
unique_lock 개체입니다.

설명

첫 번째 생성자는 연결된 뮤텍스 포인터 값 0이 포함된 개체를 생성합니다.

두 번째 생성자는 연결된 뮤텍스 상태 Other에서 이동합니다. 이동 후 Other 는 더 이상 뮤텍스와 연결되지 않습니다.

다시 기본 생성자는 저장된 포인터로 & Mtxmutex 저장합니다. mutex의 소유권은 두 번째 인수(있는 경우)에 의해 결정됩니다.

이름 설명
No argument 연결된 mutex 개체에 대해 lock 메서드를 호출하여 소유권을 가져옵니다.
Adopt 이때 소유권이 있는 것으로 가정합니다. 생성자를 호출할 때 Mtx가 잠겨 있어야 합니다.
Defer 호출 스레드는 mutex 개체를 소유하지 않는다고 가정합니다. 생성자를 호출할 때는 Mtx가 잠겨 있지 않아야 합니다.
Try 연결된 mutex 개체에 대해 try_lock을 호출하여 소유권을 가져옵니다. 생성자는 아무 항목도 throw하지 않습니다.
Rel_time try_lock_for(Rel_time)를 호출하여 소유권을 결정합니다.
Abs_time try_lock_until(Abs_time)를 호출하여 소유권을 결정합니다.

~unique_lock 소멸자

unique_lock 개체와 연결된 모든 리소스를 해제합니다.

~unique_lock() noexcept;

설명

호출 스레드가 연결된 mutex를 소유하는 경우 소멸자는 mutex 개체에 대해 unlock을 호출하여 소유권을 해제합니다.

unlock

연결된 mutex의 소유권을 해제합니다.

void unlock();

설명

호출 스레드가 연결된 mutex를 소유하고 있지 않으면 이 메서드는 오류 코드가 operation_not_permittedsystem_error를 throw합니다.

그렇지 않으면 이 메서드는 연결된 mutex에 대해 unlock을 호출하고 내부 스레드 소유권 플래그를 false로 설정합니다.

참고 항목

헤더 파일 참조
<mutex>