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

Fan*_*Fox 5 c++ multithreading mutex

用于保护std::mutex所有的c ++ 11互斥RAII类型都有一个typedef:

typedef Mutex mutex_type;
Run Code Online (Sandbox Code Playgroud)

这个成员typedef有什么意义?起初我认为它可以用来概括创建一个移动锁的对象(unique_lock例如):

template<SomeLock>
void function(SomeLock in)
    SomeLock::mutex_type newMutex;
    //Do something
Run Code Online (Sandbox Code Playgroud)

但我无法想象这个用途.

另外需要注意的是,它似乎并没有在实现中的任何地方使用locks(至少在VisualC++中没有).

会员的用例是mutex_type什么?

Cal*_*eth 1

在标准库中,每个模板参数都有一个类型别名是很正常的。顺便说一句,我记不起一个模板,因为它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, e.g. for SFINAE

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

It also allows you to easily specify a parameter of appropriate type.

template<typename Lock>
void usesMutex(Lock::mutex_type & mut);
Run Code Online (Sandbox Code Playgroud)