Pass a structure with an array to multiple threads

I was desperately trying to pass the structure to multiple threads. The problem is that I cannot find the correct format to pass it to the pthread_create function. It just won't work. If you tried to pass my struct pointers to pthread_create but it just won't work. I have pointer assignment errors.

typedef struct thread{
   pthread_t thread_id;
   char *filename;
   int blocksize;
   int random_int;  
} ThreadData;

int main(int argc, char *argv[]) {
    int nb_threads = atoi(argv[3]);
    int blocksize = atoi(argv[2]);
    int re;
    int index;
    char *name;
    char* mode = argv[1];
    int i = 0;
    ThreadData thread[nb_threads];
    int random_int;
    srand(time(NULL));  
    random_int = rand();
    index = detect_mode(mode);
    index_glob = index;

    for(i=0; i < nb_threads; i++){

        snprintf(name, 25,"temp%d",i);
        thread[i].filename = name;
        thread[i].blocksize = blocksize;
        thread[i].random_int = random_int;

        switch(index_glob){ 
        case 0:
            re = pthread_create(&(thread[i].thread_id),NULL,write_seq_file,(void *)(&thread[i]));
            break;
        case 1:
            re = pthread_create(&(thread[i].thread_id),NULL,write_ran_file,(void *)(&thread[i]));
            break;
        case 2:
            re = pthread_create(&(thread[i].thread_id),NULL,read_seq_file,(void *)(&thread[i]));
            break;
        case 3:
            re = pthread_create(&(thread[i].thread_id),NULL,read_ran_file,(void *)(&thread[i]));
            break;      
        default:
            exit(0);
            break;
        }
        if(re == -1){
            printf("Error creating thread %d / %d\n",(i+1),nb_threads);
        }
        else{
            printf("Thread %d / %d created.\n",(i+1),nb_threads);
        }
    }

    for(i=0; i< nb_threads; i++){
        pthread_join(thread[i].thread_id, NULL);
    }

    return 0;
}

void* write_seq_file(void *thread){
    ThreadData *my_data = (ThreadData*)thread;
    char *filename = my_data->filename;
    FILE* fp = fopen(filename,"w+");
    int blocksize = my_data->blocksize;
    char c = 'a';
    long i;
    for (i=0; i<blocksize; i++){
        fputc(c,fp);
    }
    pthread_exit(NULL);
}

      

+3


source to share


1 answer


I didn't quite understand your program and I'm not sure if this will work as you expect. But

Here are some points that can be compiled:

pthread_create expects void * (* start_routine) (void *) as a working function. Therefore, change the write_seq_file and all working functions to the following format.

void* write_seq(void *thread)
{
  ...
  return NULL;
}

      

Your Thread data array is an array of objects. Not pointers. So first get the address and throw it into the void.

re = pthread_create(&(thread[i].thread_id),NULL,write_seq_file,(void *)(&(thread[i])));

      

Not sure if exiting (0) in the middle is a good idea. I would rather use return 0; because the main function needs to return a value.



This code does not declare the following. So hopefully there are no problems there.

index = detect_mode(mode);
index_glob = index;

      

Make sure you have function prototypes declared before main, or move the function implementation above the main function.

Finally, pthread_join expects the pthread_t thread to be the first argument. Where the line needs to be changed as shown below.

pthread_join(thread[i].thread_id, NULL);

      

Hope it helps.

0


source







All Articles