开发者

Passing a structure which includes another structure to pthread_create

So I'm attempting to pass a structure with several variables, including another structure dealing with bitmap information. However, my code fails somewhere, as it spits out the error "dereferencing pointer to incomplete type" in regards to dereferencing pointers to information contained within the structure. I know there are many questions on here dealing with this, but I've tried to implement what was stated there and have failed.

Here's the relevant code from main() including edits regarding initialization:

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);
     }
 }

And here's the relevant code from the function being passed as an argument for pthread_create:

void开发者_StackOverflow社区* 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);
    }
}

}

And here's the structure:

struct thread_args{
    int thread_id;
    int threads;
    struct bitmap *bm;
    double xmin;
    double xmax;
    double ymin;
    double ymax;
    int max;
};


I see two problems with your code:

  1. You define struct thread_args, but in compute_image(), you use struct thread_data. These aren't the same, and (I am guessing) you meant thread_args, not thread_data. This most likely explains the compile error you got. You might consider naming this struct image_data instead; that's probably less likely to cause confusion.
  2. You initialize my_data with malloc()d memory, and then immediately reassign it from cast of threadargs. That's a memory leak, for no good reason. Just remove the malloc initialization.


This is a matter of where you place the definition of struct thread_args. When the compiler only has the prototype available it does not know anything about the struct members. You need to either keep the struct definition in the included header file, or create accessor functions in the src-file containing the definition.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜