简体   繁体   English

标准互斥或增强互斥? 哪个更好?

[英]Std mutex or boost mutex? Which is preferable?

What is the real difference between std::mutex and boost::mutex? std :: mutex和boost :: mutex之间的真正区别是什么? Which one is faster in terms of implementation and compilation? 在实现和编译方面哪个更快? Are both of them portable?I read my questions related to it but there is no clear mention of difference . 它们都是便携式的吗?我读了有关它的问题,但没有清楚地提及差异。 std mutex is supported only since c++11 so the older compilers dont support it . 仅从c ++ 11开始才支持std互斥锁,因此较早的编译器不支持它。 Are boost mutex supported by older compilers or not? 较早的编译器是否支持boost互斥锁? If the foremost condition requires the code to be portable , then what should be prefered? 如果最重要的条件要求代码具有可移植性,那么应该首选什么?

As a default choice you should prefer std:: anything to boost:: samething because it's a part of standard library and hence is more portable since it doesn't introduce external dependency. 作为默认选择,您应该更喜欢std :: 任何需要提升的地方:: samething,因为它是标准库的一部分,因此更可移植,因为它没有引入外部依赖关系。

You can't really compare std::mutex and boost::mutex in general because there is no one and only std::mutex , it's implementation depends on the standard library implementation that you are using which usually is a part of your toolchain. 通常,您无法真正比​​较std::mutexboost::mutex ,因为没有一个,只有 std::mutex ,它的实现取决于您所使用的标准库实现,通常它是工具链的一部分。

There might be a case when you discover using empirical evidence that std::mutex you are using is in some regard "worse" than boots::mutex . 在某些情况下,您可能发现使用std::mutex经验证据比boots::mutex更“糟糕”。 In this case it might be reasonable to switch to it, but only if it's really justified and you have an actual evidence (eg performance measurement) of that. 在这种情况下,切换到它可能是合理的,但前提是它确实是合理的,并且您有确凿的证据(例如性能度量)。 Even then it seems like a last resort. 即使那样,它似乎还是不得已。 It might be better to switch to a different standard library implementation or to a different toolchain or upgrade your current one if possible. 可能最好切换到其他标准库实现或其他工具链,或者升级当前的工具链。

@ std::mutex for me every time, for the reason @Henri states it is (obviously) part of the C++ standard so you can rely on it being available everywhere. @ std::mutex每次对我来说都是这样,因为@Henri声明它(显然)是C ++标准的一部分,因此您可以依靠它随处可见。

Using boost, on the other hand, means that you have to link against the boost library. 另一方面,使用boost意味着您必须链接到boost库。 While this is widely available and offers a number of handy extra features it's quite heavyweight and you wouldn't want to pull it in just for this. 尽管此功能广泛可用,并提供了许多方便的额外功能,但它却是重量级的,您不希望仅为此添加它。

Also, std::mutex may be faster. 另外, std::mutex可能更快。 The cross-platform nature of boost means that things that rely on OS support (which would include mutexes) can sometimes be less efficient. Boost的跨平台性质意味着依赖OS支持的事物(可能包括互斥体)有时效率较低。 But this would not be a major factor in my thinking. 但这不是我思考的主要因素。

But if measuring performance is important to you, you should run your own benchmark. 但是,如果衡量性能对您很重要,则应该运行自己的基准测试。 You can do this (roughly) over at (say) Wandbox - they support the boost library there. 您可以在(例如) Wandbox上 (大约)进行此操作 -他们在那里支持boost库。

Consider boost as a laboratory for prototyping std features. 考虑将boost作为标准功能原型的实验室。 Many std facilities were originally implemented on boost. 许多std设施最初是在boost上实现的。 The difference is that std takes care of consistency and forward compatiblity, while boost targets new horizons. 不同之处在于std会照顾到一致性和前向兼容性,而boost则瞄准了新的视野。 Nothing prevents boost from applying breaking changes in forth coming versions, but it also provides answers to more questions than std. 没有什么可以阻止boost在即将到来的版本中应用重大更改的,但是它提供的答案比std更多。 My personal preference is std first - when possible - and boost next - when needed. 我个人的喜好是在可能的情况下首先进行性病检查,然后在需要时进行性病检查。 I generally avoid pre c++11 platforms, unless I am forced to face. 我通常避免使用c ++ 11之前的平台,除非被迫面对。

The focus of Boost is trying new techniques and introducing new capabilities. Boost的重点是尝试新技术并引入新功能。 The focus of the C++ standard is specifying requirements in a way that (in most cases) can be implemented portably. C ++标准的重点是以可以在大多数情况下实现的方式规定要求。 A number of features from boost have found their way into the C++ standard, but were often changed quite a bit in that transition - to improve portability, sometimes improve reliability, etc. boost中的许多功能已进入C ++标准,但在此过渡过程中经常进行了很多更改-以提高可移植性,有时还提高可靠性等。

If your implementation (compiler and library) is C++11 or later, and you intend to not to port to older implementations, then definitely use std::mutex . 如果您的实现(编译器和库)是C ++ 11或更高版本,并且您打算不移植到较早的实现,则一定要使用std::mutex It is part of the standard, from 2011, so preferable. 从2011年开始,它是标准的一部分,因此更可取。 No need to rely on third-party libraries. 无需依赖第三方库。 You will only need boost if you need bleeding edge features of boost that the C++ standard does not support (which means things other than mutex ). 仅当您需要C ++标准不支持的Boost的前沿功能(这意味着mutex除外)时,才需要Boost。

Some exceptions to the above: there are some features of boost (including related to threading and mutexes) that haven't made their way into a C++ standard, and some features in the C++ standard that are not in boost. 上面的一些例外:boost的某些功能(包括与线程和互斥锁相关的功能)尚未纳入C ++标准,而C ++标准中的某些功能则未包含在boost中。

If you need to use (or support or port to) an older implementation, then consider using boost::mutex . 如果您需要使用(或支持或移植到)较旧的实现,请考虑使用boost::mutex You will, in most cases, need to install a version of boost separately, with your chosen implementation (some compiler versions have shipped with a version of boost, but don't rely on it). 在大多数情况下,您需要根据自己选择的实现分别安装boost版本,(某些编译器版本附带了boost版本,但不要依赖它)。 If there isn't a version of boost that works with your compiler/library, then (to state the obvious) you will not be able to use boost::mutex . 如果没有适用于您的编译器/库的boost版本,(显然)您将无法使用boost::mutex

Boost has had the thread library (which includes mutex ) since about version 1.25.0, which dates from late 2001. Which suggests boost is an option if your compiler is no older than (rough guess) early 2000s. Boost的线程库(包括mutex )自1.25.0版本开始,该版本可追溯到2001年末。这表明,如果您的编译器不早于(大概猜测)2000年代,则boost是一个选择。

If you need to support an implementation that is significantly older than the early 2000s, then you may be out of luck using boost::mutex , and will need to resort to other libraries/frameworks or get your hands dirty writing OS-specific code. 如果您需要支持比2000年代早得多的实现,那么使用boost::mutex可能会很不走运,并且需要诉诸其他库/框架或动手编写特定于操作系统的代码。

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

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