static void testlock()
{
for(int i=0;i<10000000;i++)
{
float f=2.0/i;
}
}
static void TEST()
{
cout<<"Start testing" <<endl;
unsigned int startClock;
for(int i=1;i<=10;i++)
{
startClock = clock();
vector<boost::thread*> threads;
for(int j=0;j<i;j++)
threads.push_back(new boost::thread(&testlock));
for(int j=0;j<i;j++)
{
threads[j]->join();
delete threads[j];
}
cout << i << " threads: "<< clock()-startClock << endl;
}
}
输出:
Start testing
1 threads: 180000
2 threads: 350000
3 threads: 540000
4 threads: 730000
5 threads: 900000
6 threads: 1080000
7 threads: 1260000
8 threads: 1510000
9 threads: 1660000
10 threads: 1810000
我在四核 PC(Core2Quad,4 核无超线程)上运行此代码,所以我预计 1-4 个线程需要大约相同的时间。相反,似乎只使用了一个核心。我在这里想念什么?
谢谢
更新:
-我在 Ubuntu Linux 下使用 Eclipse CDT
-我对 Pthread 进行了同样的尝试,得到了相同的结果