Preface: Create a thread in C language, but need to transfer multiple parameters to the thread, we would naturally think that by passing an array or struct, let's take a look at how to transfer the struct and array when creating the thread.
1#include <stdio.h>2#include <pthread.h>3#include <stdlib.h>4#include <string.h>5 6typedefstructStudent7 {8 intnum;9 Charname[Ten];Ten }info; One A void*message (void*Arg) - { -Info *p = (info*) arg; theprintf"num:%d name:%s\n",p->num,p->name); - - } - + void*read_routine1 (void*Arg) -{int*FD; +FD = (int*) arg; A //fd[0] = ((int *) arg) [0]; at //fd[1] = ((int *) arg) [1]; -printf"fd[0]:%d fd[1]:%d\n", fd[0],fd[1]); - } - - - intMainintargcChar*argv[]) in { -Info *st = (info*)malloc(sizeof(info)); toSt->num =Ten; +strcpy (St->name,"xiaoming"); - intfd[2]; thefd[0] = A; *fd[1] = +; $ pthread_t Tid1,tid2;Panax Notoginseng /*Create two threads, the first one is a struct, the second is an array*/ -Pthread_create (&tid2,null,message, (void*) st); thePthread_create (&tid1,null,read_routine1, (void*) FD); + while(1); A Free(ST); the return 0; +}
After testing, this method is feasible.
Transferring multiple parameters in a C language thread