[英]When to use defer_lock on std::shared_lock?
I see from cppreference:我从 cppreference 看到:
std::shared_lock<Mutex>::shared_lock
C++ Thread support library std::shared_lock
shared_lock() noexcept;
(1) (since C++14)
shared_lock( shared_lock&& other ) noexcept;
(2) (since C++14)
explicit shared_lock( mutex_type& m );
(3) (since C++14)
shared_lock( mutex_type& m, std::defer_lock_t t ) noexcept;
(4) (since C++14)
And:和:
4) Does not lock the associated mutex.
Well, if this defer_lock means we don't lock the mutex, then what's the usage of this lock
at all?好吧,如果这个 defer_lock 意味着我们不锁定互斥锁,那么这个lock
到底有什么用呢?
When do we need to specify defer_lock
parameter, for what kind of usage scenario?什么时候需要指定defer_lock
参数,对于什么样的使用场景?
Thanks!谢谢!
You can always lock the mutex later, using lock()
.您以后可以随时使用lock()
锁定互斥锁。
Sometimes, due to scoping rules, or whatever, you don't want to immediately lock anything when the lock object is created;有时,由于范围规则或其他原因,您不想在创建锁 object 时立即锁定任何内容; but you still want to have the lock released, automatically, upon leaving the declaration's scope.但是您仍然希望在离开声明的 scope 时自动释放锁。
This allows you to instantiate a lock object, and not lock anything right away, but lock()
it later.这允许您实例化一个锁 object,而不是立即锁定任何东西,而是稍后lock()
它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.