2013-02-19 85 views
0

如何访问在runner函数中main()中动态声明的变量和矩阵。 我将它们作为参数传递给runner,但我不确定它是否正确,因为我必须在pthread_create函数调用中传递runner。我将不得不将所有参数传递给跑步者吗?我该怎么做 ?如何在调用另一个函数时传递函数的参数?

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); 
    } 

回答

3

线程功能只从并行线程获得一个参数,一个void *

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

解决这个是定义一个struct,实例化并初始化其与所希望的值的方式,那么将一个指向结构的指针传递给pthread_create()

该指针是上述原型中的void *arg。手册页说:

新线程开始执行,调用start_routine(); arg作为start_routine()的唯一参数 传递。

+0

另外,在你的结构中,你可以简单地持有一个指向你的数据的指针,而不是整个矩阵本身的副本,因为它是在main中动态创建的。 – 75inchpianist 2013-02-19 16:03:53

+0

谢谢,我明白了,我会尽力实现它 – Learner 2013-02-19 16:12:11

相关问题