Итак, я пытаюсь передать структуру с несколькими переменными, включая другую структуру, имеющую дело с информацией растрового изображения.Однако мой код где-то дает сбой, поскольку он выдает ошибку «разыменование указателя на неполный тип» в отношении разыменования указателей на информацию, содержащуюся в структуре.Я знаю, что здесь есть много вопросов, связанных с этим, но я попытался реализовать то, что там было сказано и потерпело неудачу.
Вот соответствующий код из 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;
};