c passing several arguments to threads

前端 未结 4 437
暗喜
暗喜 2021-01-14 21:21

when i create a thread, i want to pass several arguments. So i define in a header file the following:

struct data{
  char *palabra;
  char *directorio;
  FI         


        
4条回答
  •  天命终不由人
    2021-01-14 21:43

    Create a pointer to a struct like you do in the first case above:

    //create a pointer to a struct of type data and allocate memory to it
    struct data *info
    info = malloc(sizeof(struct data));
    
    //set its fields
    info->palabra   = ...;
    info->directoro = ...;
    
    //call pthread_create casting the struct to a `void *`
    pthread_create ( &thread_id[i], NULL, &hilos_hijos, (void *)data);
    

提交回复
热议问题