我正在创建一个测试程序来测试CPU性能和温度,同时CPU使用率介于70%到80%之间。我正在使用线程在循环中运行计算并不停止。但是CPU增加到100%。我不知道维持使用率不高于80%,但高于70%。我可以提供一些建议吗?创建一个C程序来加载CPU利用率> 70%但低于80%
void *doCalculate(void *a);
int main(void)
{
pthread_t threads[NUM_THREAD];
int thread_args[NUM_THREAD];
int i,s;
//Create Threads
for (i=0; i<NUM_THREAD; ++i)
{
thread_args[i] = i;
printf("spawning thread %d\n", i);
s=pthread_create(&threads[i], NULL, doCalculate, (void *)
&thread_args[i]);
if(s!=0)
handle_error(s,"pthread_create");
}
//Wait for the threads to finish
for (i=0; i<NUM_THREAD; ++i) {
pthread_join(threads[i], NULL);
}
return 1;
}
void *doCalculate(void *a){
int tid;
tid = *((int *) a);
printf("Thread %d!\n", tid);
float x = 1.5f;
while (1)
{
x *= sin(x)/atan(x) * tanh(x) ;
}
}
这是我的临时解决方案。我在函数doCalculation中放置了printf和for循环。
void *doCalculate(void *a){
int tid;
tid = *((int *) a);
printf("Thread %d!\n", tid);
float x = 1.5f;
while (1)
{
int i=0;
printf("\t\n\n");
x *= sin(x)/atan(x) * tanh(x) ;
for(;i<15000;i++){}
}
}
这里没有代码 – Sathiya
我的道歉。请看看, –
ÓK。我可能会找到解决方案。我把printf和for循环放在函数doCalculate中。 –