1
votes

We know that we call pthread like this

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, 
                    void *(*start_routine) (void *), void* arg);

However, if in the start_routine function I wanna call has more than one argument, what can I do?

1
I'm thinking you mean "argument" instead of "augment". - Vaughn Cato

1 Answers

4
votes

You can put whatever you want into a struct and pass a pointer to that.

In C:

typedef struct {
  int a;
  int b;
} ChildMainArgs;

void child_main(int a,int b);

void child_main_thread(void *arg)
{
  ChildMainArgs *args_ptr = (ChildMainArgs *)arg;
  child_main(args_ptr->a,args_ptr->b);
}

ChildMainArgs args;
args.a = 5;
args.b = 7;
pthread_create(..,..,child_main_thread,&args);