头文件中的内联std :: mutex

洛雷托

我正在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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章