我不断收到跟踪错误,但我不知道它们的真正含义。
/tmp/ccIUS3m4.o:在函数main':
assign3_1.c:(.text+0x7f): undefined reference to
pthread_create中
Assign3_1.c :(。text + 0xa9):对pthread_join'
/tmp/ccIUS3m4.o: In function
runner'的未定义引用:
Assign3_1.c :(。text + 0x1e6):对'sqrt'的未定义引用
collect2:错误:ld返回1退出状态
下面是我的代码:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define NUMBER_OF_POINTS 1000000
#define NUMBER_OF_THREADS 2
void *runner(void *param);
// circle size
int circle_count=0;
// generate a double precision random number
double random_double(){
return random() / ((double)RAND_MAX + 1);
}
int main (int argc, const char *argv[]){
int points_per_thread = NUMBER_OF_POINTS/NUMBER_OF_THREADS;
int i;
double Pi;
pthread_t workers[NUMBER_OF_THREADS];
//create random numbers
srandom((unsigned)time(NULL));
clock_t begin = clock();
for (i=0; i < NUMBER_OF_THREADS; i++)
pthread_create(&workers[i], 0, runner, &points_per_thread);
for(i=0; i < NUMBER_OF_THREADS; i++)
pthread_join(workers[i],NULL);
//estimating Pi
Pi = 4.0 * circle_count / NUMBER_OF_POINTS;
clock_t end = clock();
double time_spent = (double)(end - begin)/CLOCKS_PER_SEC;
printf("NUMBER OF POINTS = %d\n", NUMBER_OF_POINTS);
printf("Pi = %f\n",Pi);
printf("time = %f\n",time_spent);
return 0;
}
void *runner(void *param){
int POINTS;
POINTS = *((int *)param);
int i;
int hit_count = 0;
double x, y;
for(i=0; i < POINTS; i++){
//generate numbers b/w -1.0 to +1.0
//to obtain a random (x, y) coordinates
x = random_double() * 2.0 - 1.0;
y = random_double() * 2.0 - 1.0;
//are the points within the circle?
if (sqrt(x*x + y*y) < 1.0)
++hit_count; //yes
}
circle_count += hit_count;
pthread_exit(0);
}