Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create pthread with the function of multiple arguments

Tags:

c

pthreads

If I am going to create a pthread for the following function.

Assume everything is properly delared.

pthread_create(&threadId, &attr, (void * (*)(void*))function, //what should be the arguments for here??);
int a = 0;
int b = 1;
//c and d are global variables.

void function(int a, int b){
    c = a;
    d = b;
}
like image 350
Renju Liu Avatar asked Feb 14 '23 17:02

Renju Liu


1 Answers

This does not work. function() has to take exactly one argument. That's why you have to do this:

(void * ()(void))

You're telling your compiler "no, seriously, this function only takes one argument", which of course it doesn't.

What you have to do instead is pass a single argument (say a pointer to a struct) which gets you the information you need.

Edit: See here for an example: number of arguments for a function in pthread

like image 176
Peter Crabtree Avatar answered Mar 02 '23 16:03

Peter Crabtree