对于下面的代码,我的CPU使用率为97%。我在Ubuntu上运行c代码。多线程导致CPU使用率更高
#include <stdio.h>
#include<pthread.h>
void *s_thread(void *x)
{
printf("I am in first thread\n");
}
void *f_thread(void *x)
{
printf("I am in second thread\n");
}
int main()
{
printf("I am in main\n");
pthread_t fth;
int ret;
ret = pthread_create(&fth,NULL,f_thread,NULL);
ret = pthread_create(&sth,NULL,s_thread,NULL);
while(1);
return 0;
}
这个简单的代码比只运行一个线程给了我更多的cpu使用。
你可以请张贴你试过的单线程版本。 – ibid
我很困惑。你编写了一个无限循环'while(1);'然后......想知道为什么只需要97%的CPU?当你到达无限循环时,你的线程已经死了,他们会在哪里帮助?我很困惑你想完成什么。 – sarnold
这是一个简单的例子,我计算出来,但假设我的线程也包含一个无限循环... – Happy