std::shared_mutex:: lock_shared
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Exclusive locking | ||||
| Shared locking | ||||
|
shared_mutex::lock_shared
|
||||
| Native handle | ||||
|
void
lock_shared
(
)
;
|
(C++17부터) | |
뮤텍스의 공유 소유권을 획득합니다. 다른 스레드가 뮤텍스를 배타적 소유권으로 보유 중인 경우,
lock_shared
호출은 공유 소유권을 획득할 수 있을 때까지 실행을 차단합니다.
만약
lock_shared
를 이미
mutex
를 어떤 모드(독점 또는 공유)로든 소유하고 있는 스레드가 호출하면, 그 동작은 정의되지 않습니다.
공유 모드에서 뮤텍스를 이미 잠근 공유 소유자의 수가 구현에서 정의된 최대 수를 초과하는 경우,
lock_shared
는 공유 소유자 수가 감소할 때까지 실행을 차단합니다. 최대 소유자 수는 최소 10000 이상임이 보장됩니다.
동일한 뮤텍스에 대한 이전 unlock() 연산은 이 연산과 동기화됩니다 ( std::memory_order 에 정의된 대로).
목차 |
매개변수
(없음)
반환값
(없음)
예외
오류가 발생할 경우
std::system_error
를 발생시킵니다. 여기에는
lock
이 명세를 충족하는 것을 방해하는 기본 운영 체제의 오류도 포함됩니다. 예외가 발생한 경우 뮤텍스는 잠기지 않은 상태입니다.
참고 사항
lock_shared()
는 일반적으로 직접 호출되지 않습니다:
std::shared_lock
이 공유 잠금을 관리하는 데 사용됩니다.
예제
#include <chrono> #include <iostream> #include <mutex> #include <shared_mutex> #include <syncstream> #include <thread> #include <vector> std::mutex stream_mutx; void print(auto 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); product = data; } std::this_thread::sleep_for(3ms); } smtx.lock(); // 수동 잠금 product = LAST; smtx.unlock(); }; auto reader = [&smtx, &product]() { int data = 0; std::vector<int> seen; do { { smtx.lock_shared(); // std::shared_lock lock(smtx); 사용 권장 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) |