简体   繁体   English

在`unique_lock`,`scoped_lock`和`lock_guard`中指定的mutex_type的用例是什么?

[英]What is the use case for mutex_type specified in `unique_lock`, `scoped_lock` and `lock_guard`?

The c++11 mutex RAII types for guarding std::mutex all have a typedef: 用于保护std::mutex的c ++ 11互斥RAII类型都有一个typedef:

typedef Mutex mutex_type;

What is the point of this member typedef? 这个成员typedef有什么意义? At first I thought it could be used to generalize creating an object for moving the lock (in the case of the unique_lock ) for example: 起初我认为它可以用于概括创建一个移动锁的对象(例如unique_lock ):

template<SomeLock>
void function(SomeLock in)
    SomeLock::mutex_type newMutex;
    //Do something

But I cannot imagine a use for this. 但我无法想象这个用途。

A further note is that it doesn't appear to be used anywhere in the implementation of the locks (at least not in VisualC++). 另外需要注意的是,它似乎没有在locks的实现中的任何地方使用(至少在VisualC ++中没有)。

What is a use case of the member mutex_type ? 成员mutex_type的用例是什么?

Having a type alias for each template parameter is normal in the standard library. 在标准库中具有每个模板参数的类型别名是正常的。 Off hand, I can't recall a template in std that doesn't alias all it's template parameters as member types 另外,我无法回想起std中的模板, 它不会将所有模板参数作为成员类型别名

Having a distinct name for a type alias in a group of related classes allows for that group to be easily distinguished from other classes, eg for SFINAE 在一组相关类中具有类型别名的不同名称允许该组容易与其他类区分开,例如对于SFINAE

template<typename Lock, typename = std::void_t<Lock::mutex_type>>
void usesLock(Lock lock); // Substitution failure for most instantiations of Lock

It also allows you to easily specify a parameter of appropriate type. 它还允许您轻松指定适当类型的参数。

template<typename Lock>
void usesMutex(Lock::mutex_type & mut);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM