简体   繁体   English

为什么boost :: mutex比vs2013的std :: mutex更快?

[英]Why is boost::mutex faster than std::mutex as of vs2013?

Today I wrote some code to test the performance of mutex. 今天我写了一些代码来测试互斥锁的性能。

This is the boost(1.54) version, compiled on vs2010 with O2 optimization: 这是boost(1.54)版本,在vs2010上使用O2优化编译:

boost::mutex m;
auto start = boost::chrono::system_clock::now();
for (size_t i = 0; i < 50000000; ++i) {
    boost::lock_guard<boost::mutex> lock(m);
}
auto end = boost::chrono::system_clock::now();
boost::chrono::duration<double> elapsed_seconds = end - start;
std::cout << elapsed_seconds.count() << std::endl;

And this is the std version, compiled on VS2013, with O2 optimization too: 这是在VS2013上编译的std版本,也有O2优化:

std::mutex m;
auto start = std::chrono::system_clock::now();
for (size_t i = 0; i < 50000000; ++i) {
    std::lock_guard<std::mutex> lock(m);
}
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << elapsed_seconds.count() << std::endl;

A bit different but doing just the same thing. 有点不同但做同样的事情。 My CPU is Intel Core i7-2600K, my OS is Windows 7 64bit, and the result is: 0.7020s vs 2.1684s, 3.08 times. 我的CPU是Intel Core i7-2600K,我的操作系统是Windows 7 64bit,结果是:0.7020s vs 2.1684s,3.08倍。

boost::mutex will try _interlockedbittestandset first, and if it failed, the big cheese WaitForSingleObject will come second, it's simple to understand. boost :: mutex将首先尝试_interlockedbittestandset,如果失败,那么大奶酪WaitForSingleObject将排在第二位,它很容易理解。

It seems that std::mutex of VS2013 is much more complex, I have already tried to understand it but I could not get the point, why it's so complex ? 似乎VS2013的std :: mutex要复杂得多,我已经尝试了解它,但我无法理解它,为什么它如此复杂? is there a faster way ? 有更快的方法吗?

It seems that stl::mutex might only use system calls, which take a LOT of overhead; 似乎stl::mutex可能只使用系统调用,这需要很多开销; but boost::mutex implements at least some of its functionality programmatically -- ie it tries to avoid system calls whenever possible, which would be the reason for the try _interlockedbittestandset check before WaitForSingleObject . 但是boost::mutex至少以编程方式实现了它的一些功能 - 即它尽可能避免系统调用,这就是在WaitForSingleObject之前检查try _interlockedbittestandset的原因。

I don't know the actual internals of MS's stl, but I've seen performance differences like this from examples in an operating systems class. 我不知道MS的stl的实际内部结构,但是我从操作系统类的例子中看到了这样的性能差异。

The test is only testing the condition of locking an unlocked mutex without any contentions from other threads. 该测试仅测试锁定未锁定互斥锁的条件,而不会与其他线程产生任何争用。

Let's say the mutex was locked. 假设互斥锁被锁定了。 After the initial boost try, would it then be better for the thread to spin or block? 在初始加速尝试之后,线程旋转或阻塞会更好吗? It really all depends on the application. 这完全取决于应用程序。 And maybe the stl one performs better under heavy load. 也许stl one在重负载下表现更好。

When times calls for a highly-efficient mutex, a lock-free alternative to achieve the same goals is worth exploring. 当时代需要高效的互斥锁时,实现相同目标的无锁替代方案值得探索。

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

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