我编写了一个简单的测试程序来产生一些处理器负载。它将抛出 6 个线程并在每个线程 pi 中进行计算。但是处理器在目标平台(arm)上只生成 3 个线程,普通 Linux-PC 上的同一个程序会生成全部 6 个线程。
问题是什么?
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define ITERATIONS 10000000000000
#define NUM_THREADS 6
void *calculate_pi(void *threadID) {
double i;
double pi;
int add = 0;
pi = 4;
for (i = 0; i < ITERATIONS; i++) {
if (add == 1) {
pi = pi + (4/(3+i*2));
add = 0;
} else {
pi = pi - (4/(3+i*2));
add = 1;
}
}
printf("pi from thread %d = %20lf in %20lf iterations\n", (int)threadID, pi, i);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
int i;
for ( i = 0 ; i < NUM_THREADS; i++) {
rc = pthread_create(&threads[i], NULL, calculate_pi, (void *)i);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(EXIT_FAILURE);
}
}
for ( i = 0 ; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
return(EXIT_SUCCESS);
}