所以我试图传递一个包含多个变量的结构,包括另一个处理位图信息的结构。但是,我的代码在某处失败,因为它在取消引用指向结构中包含的信息的指针时吐出错误“取消引用指向不完整类型的指针”。我知道这里有很多问题要处理这个问题,但我试图实施那里所说的并且失败了。
这是来自 main() 的相关代码,包括有关初始化的编辑:
pthread_t threads[thread_num];
pthread_attr_t attr;
int rc;
void *status;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
// Create a bitmap of the appropriate size.
struct bitmap *bitm = bitmap_create(image_width,image_height);
struct thread_args *arguments = (struct thread_args*) malloc(sizeof(struct thread_args));
arguments->bm = bitm;
arguments->xmin = xcenter-scale;
arguments->xmax = xcenter+scale;
arguments->ymin = ycenter-scale;
arguments->ymax = ycenter+scale;
arguments->max = max;
// Compute the Mandelbrot image
for(int i=0;i<thread_num;i++){
arguments->thread_id = i;
if(pthread_create(&threads[i], NULL, compute_image, (void *)arguments)<0){
printf("ERROR; return code from pthread_create() is %d\n", rc);
}
}
pthread_attr_destroy(&attr);
for(int t=0; t<thread_num; t++) {
rc = pthread_join(threads[t], &status);
if (rc) {
printf("ERROR; return code from pthread_join() is %d\n", rc);
exit(-1);
}
}
以下是作为 pthread_create 参数传递的函数的相关代码:
void* compute_image(void *threadargs ){
int i,j;
struct thread_data *my_data = (struct thread_args*) malloc(sizeof(struct thread_args));
my_data = (struct thread_data *) threadargs;
int width = bitmap_width(my_data->bm);
int height = bitmap_height(my_data->bm);
int threads = my_data->threads;
int thread_id = my_data->thread_id;
double xmin = my_data->xmin;
double xmax = my_data->xmax;
double ymin = my_data->ymin;
double ymax = my_data->ymax;
int max = my_data->max;
// For every pixel in the image...
for(j=height/threads*thread_id;j<height/threads*(thread_id+1);j++) {
for(i=0;i<width;i++) {
// Determine the point in x,y space for that pixel.
double x = xmin + i*(xmax-xmin)/width;
double y = ymin + j*(ymax-ymin)/height;
// Compute the iterations at that point.
int iters = iterations_at_point(x,y,max);
// Set the pixel in the bitmap.
bitmap_set(my_data->bm,i,j,iters);
}
}
}
这是结构:
struct thread_args{
int thread_id;
int threads;
struct bitmap *bm;
double xmin;
double xmax;
double ymin;
double ymax;
int max;
};