我正在std::mutex
不同的cpp文件中使用全局。
可以在头文件中将其声明为inline
吗?
inline std::mutex mtx;
是这样mtx
构造的吗?
是否应该显式初始化?如:
inline std::mutex mtx = {};
在inline
适用于变量(C ++ 17)的关键字文档(https://en.cppreference.com/w/cpp/language/inline)中指出
2) It has the same address in every translation unit.
和
If an inline function or variable (since C++17) with external linkage is defined differently in different translation units, the behavior is undefined.
我从这些句子中了解到,互斥锁实际上是唯一的且已正确初始化(如果使用了建议的唯一标头)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句