似乎 CRITICAL_SECTION 性能在 Windows 8 及更高版本上变得更糟。(见下图)
测试非常简单:一些并发线程每个执行 300 万个锁来独占访问一个变量。您可以在问题的底部找到 C++ 程序。我在 Windows Vista、Windows 7、Windows 8、Windows 10(x64、VMWare、Intel Core i7-2600 3.40GHz)上运行测试。
结果如下图所示。X 轴是并发线程数。Y 轴是以秒为单位的经过时间(越低越好)。
我们可以看到:
SRWLock
所有平台的性能大致相同CriticalSection
在 Windows 8 及更高版本上,性能相对 SRWL 变得更差
问题是:谁能解释为什么 CRITICAL_SECTION 性能在 Win8 及更高版本上变得更差?
一些注意事项:
- 真实机器上的结果几乎相同 - CS 在 Win8 及更高版本上比 std::mutex、std::recursive_mutex 和 SRWL 差得多。但是我没有机会在具有相同 CPU 的不同操作系统上运行测试。
std::mutex
Windows Vista 的实现基于CRITICAL_SECTION
,但 Win7 及更高版本std::mutex
基于 SWRL。对于 MSVS17 和 15 都是正确的(确保primitives.h
在安装 MSVC++ 时搜索文件并查找stl_critical_section_vista
和stl_critical_section_win7
类)这解释了 Win Vista 和其他系统上 std::mutex 性能之间的差异。- 正如评论中所说,这
std::mutex
是一个包装器,因此对于一些相对于SRWL的开销的可能解释可能是包装器代码引入的开销。
#include <chrono>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
#include <Windows.h>
const size_t T = 10;
const size_t N = 3000000;
volatile uint64_t var = 0;
const std::string sep = ";";
namespace WinApi
{
class CriticalSection
{
CRITICAL_SECTION cs;
public:
CriticalSection() { InitializeCriticalSection(&cs); }
~CriticalSection() { DeleteCriticalSection(&cs); }
void lock() { EnterCriticalSection(&cs); }
void unlock() { LeaveCriticalSection(&cs); }
};
class SRWLock
{
SRWLOCK srw;
public:
SRWLock() { InitializeSRWLock(&srw); }
void lock() { AcquireSRWLockExclusive(&srw); }
void unlock() { ReleaseSRWLockExclusive(&srw); }
};
}
template <class M>
void doLock(void *param)
{
M &m = *static_cast<M*>(param);
for (size_t n = 0; n < N; ++n)
{
m.lock();
var += std::rand();
m.unlock();
}
}
template <class M>
void runTest(size_t threadCount)
{
M m;
std::vector<std::thread> thrs(threadCount);
const auto start = std::chrono::system_clock::now();
for (auto &t : thrs) t = std::thread(doLock<M>, &m);
for (auto &t : thrs) t.join();
const auto end = std::chrono::system_clock::now();
const std::chrono::duration<double> diff = end - start;
std::cout << diff.count() << sep;
}
template <class ...Args>
void runTests(size_t threadMax)
{
{
int dummy[] = { (std::cout << typeid(Args).name() << sep, 0)... };
(void)dummy;
}
std::cout << std::endl;
for (size_t n = 1; n <= threadMax; ++n)
{
{
int dummy[] = { (runTest<Args>(n), 0)... };
(void)dummy;
}
std::cout << std::endl;
}
}
int main()
{
std::srand(time(NULL));
runTests<std::mutex, WinApi::CriticalSection, WinApi::SRWLock>(T);
return 0;
}
测试项目在 Microsoft Visual Studio 17 (15.8.2) 上构建为 Windows 控制台应用程序,具有以下设置:
- MFC 的使用:在静态库中使用 MFC
- Windows SDK 版本:10.0.17134.0
- 平台工具集:Visual Studio 2017 (v141)
- 优化:O2、Oi、Oy-、GL