【发布时间】:2014-05-30 09:55:36
【问题描述】:
我正在关注这个关于 pthreads 的教程:
https://computing.llnl.gov/tutorials/pthreads/#Abstract
还有一个通过结构体传递多个参数的例子:
struct thread_data{
int thread_id;
int sum;
char *message;
};
struct thread_data thread_data_array[NUM_THREADS];
int main (int argc, char *argv[])
{
...
thread_data_array[t].thread_id = t;
thread_data_array[t].sum = sum;
thread_data_array[t].message = messages[t];
rc = pthread_create(&threads[t], NULL, PrintHello, (void *) &thread_data_array[t]);
...
}
这个例子展示了如何不传递单个参数:
int rc;
long t;
for(t=0; t<NUM_THREADS; t++)
{
printf("Creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *) &t);
...
}
我不明白为什么最后一个场景是错误的,因为它传递了t的地址。但是第一种情况不是通过thread_data 结构的地址吗?
【问题讨论】:
标签: c linux pointers pthreads posix