Learner
Learner

Reputation: 79

How do I pass parameters of a function when it is passed in calling of another function?

How do I access variables and matrices declared dynamically in main () in runner function. I passed them as a parameter in runner but I am not sure if it is correct because i would have to pass runner in pthread_create function call. Will I have to give all parameters I passed in runner while passing it into runner ? How do I do it ?

main() {
        int  m, n, p, q
        int **a, **b, **c;
    ... // dynamically allocating first, second and multiply matrices and taking values    
           // m , n , p , q from user or a file.
    ...
r= pthread_create(threads[i], NULL, runner, (void*) &rows[i]);} // should I specify the                
  // parameters of runner in it ?

 void *runner (int **a, int **b, int **c, int m, int n, int p ) // is it right ???
 { 
        .... using all parameters
pthread_exit(NULL);
  }

Upvotes: 0

Views: 87

Answers (1)

unwind
unwind

Reputation: 400129

A thread function only gets a single argument from pthreads, a void *:

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

The way to solve this is to define a struct, instantiate it and initialize it with the desired values, then pass a pointer to the structure to pthread_create().

This pointer is the void *arg in the prototype above. The manual page says:

The new thread starts execution by invoking start_routine(); arg is passed as the sole argument of start_routine().

Upvotes: 3

Related Questions