【发布时间】:2014-06-11 08:15:53
【问题描述】:
我有这个代码:
#define threadsNum 4
DWORD WINAPI func(LPVOID vpParam)
{
long long sum = 0;
for(int i = 0; i < 400000 / threadsNum; i++)
{
for(int j = 0; j < 160000 / threadsNum; j++)
{
sum = sum > 1000 ? 0 : sum + 1;
}
}
return 1;
}
int main()
{
clock_t timer = clock();
int CPUs = 4;
DWORD_PTR threadCore = 1;
DWORD_PTR threadID = 0;
int addNum = 0;
void* *threads = new void*[threadsNum];
for (int i = 0; i < threadsNum; i++)
{
threadCore = 1 << addNum;
addNum++;
if (addNum == 4)
addNum = 0;
threads[i] = CreateThread(0, 0, func, NULL , 0, &threadID);
SetThreadAffinityMask(threads[i], threadCore);
}
if (WaitForMultipleObjects(threadsNum, threads, true, INFINITE) == WAIT_FAILED)
FatalAppExitA(NULL, "FAIL");
cout<<clock() - timer<<endl;
getchar();
return 1;
}
我的电脑上有 4 个内核。随着threadsNum 的数量增加,时间变小。当threadsNum等于4时,输出为22325,当为8时,输出为11549。为什么?每个核心都做同样的工作。对于threadsNum = 8,每个内核都有 2 个线程,当threadsNum = 4 时它们一起做同样的工作。那么为什么它更快呢?
【问题讨论】:
-
线程可能被抢占并且可能正在执行一些 IO...
标签: c++ windows multithreading performance