c passing several arguments to threads

前端 未结 4 441
暗喜
暗喜 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:39

    1) you need to use malloc and not define like below

    struct data *info;
    info = (struct data *)malloc(sizeof(struct data));
    

    and pass the pointer of the structure in ptherad call as below

    pthread_create ( &thread_id[i], NULL, &thread_fn, (void *)info );
    

    2) you can access them in thread function as below

    void thread_function ( void *arguments){
    
    struct data *info = (struct data *)arguments;
    
    info->....
    
    }
    

提交回复
热议问题