首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::shared_lock::shared_lock

shared_lock();

(1)

(since C++14)

shared_lock( shared_lock&& other );

(2)

(since C++14)

explicit shared_lock( mutex_type& m );

(3)

(since C++14)

shared_lock( mutex_type& m, std::defer_lock_t t );

(4)

(since C++14)

shared_lock( mutex_type& m, std::try_to_lock_t t );

(5)

(since C++14)

shared_lock( mutex_type& m, std::adopt_lock_t t );

(6)

(since C++14)

template< class Rep, class Period > shared_lock( mutex_type& m, const std::chrono::duration<Rep,Period>& timeout_duration );

(7)

(since C++14)

template< class Clock, class Duration > shared_lock( mutex_type& m, const std::chrono::time_point<Clock,Duration>& timeout_time );

(8)

(since C++14)

构造一个shared_lock,可以选择锁定所提供的互斥对象。

1%29构造一个shared_lock没有关联的互斥物。

2%29移动构造函数。初始化shared_lock带着...的内容otherother没有关联的互斥物。

3-8%29构造ashared_lock带着m作为相关的互斥体。此外:

3%29通过调用m.lock_shared()如果此线程已经在任何模式下拥有互斥对象,则该行为是未定义的。

4%29不锁定相关的互斥体。

5%29尝试将相关互斥锁在共享模式下,而不通过调用阻止。m.try_lock_shared()如果此线程已经在任何模式下拥有互斥对象,则该行为是未定义的。

6%29假定调用线程已经拥有m在共享模式下。

7%29尝试通过调用m.try_lock_shared_until(timeout_duration),它将一直阻塞直到指定。timeout_duration已过或已取得锁,以第一位为准。可能阻塞时间超过timeout_duration如果此线程已经在任何模式下拥有互斥对象,则该行为是未定义的。

8%29尝试通过调用m.try_lock_shared_for(timeout_time),它将一直阻塞直到指定。timeout_time已到达或已获得锁,两者以第一位为准。可能会阻塞更长的时间直到timeout_time已经联系到了。如果此线程已经在任何模式下拥有互斥对象,则该行为是未定义的。

参数

other

-

another shared_lock to initialize the state with

m

-

mutex to associate with the lock and optionally acquire ownership of

t

-

tag parameter used to select constructors with different locking strategies

timeout_duration

-

maximum duration to block for

timeout_time

-

maximum time point to block until

例外

1,2,4%29

noexcept规格:

noexcept

二次

代码语言:javascript
复制
#include <shared_mutex>
#include <iostream>
#include <thread>
#include <chrono>
 
std::shared_timed_mutex m;
int i = 10;
 
void read()
{
   // both the threads get access to the integer i
   std::shared_lock<std::shared_timed_mutex> slk(m);
   std::cout << "read i as " << i << "...\n"; // this is not synchronized
   std::this_thread::sleep_for(std::chrono::milliseconds(10));
   std::cout << "woke up...\n";
}
 
int main()
{
   std::thread r1(read);
   std::thread r2(read);
 
   r1.join();
   r2.join();
   return 0;
}

二次

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com