linux c 多核cpu编程,在Linux上,PThreads &多核 CPU_c++_开发99编程知识库

我在写一个简单的应用程序,它使用Threads来提高性能。 问题是,此应用程序运行良好的在windows上,使用我的CPU有2 个内核。 但是当我上执行Linux,似乎只有使用1 Core 。

我不理解为什么发生这种情况。

这是我的代码,c++:#include

#include

#include

#include

void* function(void*)

{

int i=0;

for(i=0; i<1110111; i++)

rand();

return 0;

}

void withOutThreads(void)

{

function(0);

function(0);

}

void withThreads(void)

{

pthread_t* h1 = new pthread_t;

pthread_t* h2 = new pthread_t;

pthread_attr_t* atr = new pthread_attr_t;

pthread_attr_init(atr);

pthread_attr_setscope(atr,PTHREAD_SCOPE_SYSTEM);

pthread_create(h1,atr,function,0);

pthread_create(h2,atr,function,0);

pthread_join(*h1,0);

pthread_join(*h2,0);

pthread_attr_destroy(atr);

delete h1;

delete h2;

delete atr;

}

int main(void)

{

int ini,tim;

ini = clock();

withOutThreads();

tim = (int) ( 1000*(clock()-ini)/CLOCKS_PER_SEC );

printf("Time Sequential: %d msn",tim);

fflush(stdout);

ini = clock();

withThreads();

tim = (int) ( 1000*(clock()-ini)/CLOCKS_PER_SEC );

printf("Time Concurrent: %d msn",tim);

fflush(stdout);

return 0;

}

输出在Linux :Time Sequential: 50 ms

Time Concurrent: 1610 ms

输出在Windows :Time Sequential: 50 ms

Time Concurrent: 30 ms

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值