std::shared_mutex:: lock
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Exclusive locking | ||||
|
shared_mutex::lock
|
||||
| Shared locking | ||||
| Native handle | ||||
|
void
lock
(
)
;
|
(C++17부터) | |
shared_mutex
의 배타적 소유권을 획득합니다. 다른 스레드가 동일한
shared_mutex
에 대해 배타적 락이나 공유 락을 보유 중인 경우,
lock
호출은 모든 해당 락이 해제될 때까지 실행을 차단합니다.
shared_mutex
가 배타적 모드로 잠겨 있는 동안에는 다른 어떤 종류의 락도 동시에 보유될 수 없습니다.
만약
lock
이 이미
shared_mutex
를 어떤 모드(독점 또는 공유)로든 소유하고 있는 스레드에 의해 호출되면, 그 동작은 정의되지 않습니다.
동일한 뮤텍스에 대한 이전
unlock()
연산은 이 연산과
동기화됩니다
(
std::memory_order
에 정의된 대로).
목차 |
매개변수
(없음)
반환값
(없음)
예외
오류가 발생하면
std::system_error
를 발생시킵니다. 여기에는
lock
이 명세를 충족하는 것을 방해하는 기본 운영 체제의 오류도 포함됩니다. 예외가 발생한 경우 뮤텍스는 잠기지 않습니다.
참고 사항
lock()
는 일반적으로 직접 호출되지 않습니다:
std::unique_lock
,
std::scoped_lock
, 그리고
std::lock_guard
가 배타적 잠금 관리를 위해 사용됩니다.
예제
#include <chrono> #include <iostream> #include <mutex> #include <shared_mutex> #include <syncstream> #include <thread> #include <vector> std::mutex stream_mutx; void print(auto const& v) { std::unique_lock<std::mutex> lock(stream_mutx); std::cout << std::this_thread::get_id() << " saw: "; for (auto e : v) std::cout << e << ' '; std::cout << '\n'; } int main() { using namespace std::chrono_literals; constexpr int N_READERS = 5; constexpr int LAST = -999; std::shared_mutex smtx; int product = 0; auto writer = [&smtx, &product](int start, int end) { for (int i = start; i < end; ++ i) { auto data = i; { std::unique_lock<std::shared_mutex> lock(smtx); // better than: // smtx.lock(); product = data; } std::this_thread::sleep_for(3ms); } smtx.lock(); // lock manually product = LAST; smtx.unlock(); }; auto reader = [&smtx, &product] { int data = 0; std::vector<int> seen; do { { // better to use: std::shared_lock lock(smtx); // smtx.lock_shared(); data = product; } // smtx.unlock_shared(); seen.push_back(data); std::this_thread::sleep_for(2ms); } while (data != LAST); print(seen); }; std::vector<std::thread> threads; threads.emplace_back(writer, 1, 13); threads.emplace_back(writer, 42, 52); for (int i = 0; i < N_READERS; ++ i) threads.emplace_back(reader); for (auto&& t : threads) t.join(); }
가능한 출력:
127755840 saw: 43 3 3 4 46 5 6 7 7 8 9 51 10 11 11 12 -999 144541248 saw: 2 44 3 4 46 5 6 7 7 8 9 51 10 11 11 12 -999 110970432 saw: 42 2 3 45 4 5 47 6 7 8 8 9 10 11 11 12 -999 119363136 saw: 42 2 3 4 46 5 6 7 7 8 9 9 10 11 11 12 12 -999 136148544 saw: 2 44 3 4 46 5 6 48 7 8 9 51 10 11 11 12 12 -999
참고 항목
|
뮤텍스를 잠그려 시도하며, 뮤텍스를 사용할 수 없는 경우 반환합니다
(public member function) |
|
|
뮤텍스의 잠금을 해제합니다
(public member function) |
|
|
공유 소유권을 위해 뮤텍스를 잠그며, 뮤텍스를 사용할 수 없는 경우 차단됩니다
(public member function) |