【发布时间】:2018-07-11 08:33:41
【问题描述】:
所以我正在通过 POSIX pthreads 深入研究 c 中的多线程,但我确实对指针的一般概念及其引用和解除引用机制感到困惑。
中的参数之一
pthread_create(...,pthread_attr_t *attr,...)
是一个函数指针。
这个函数通常是这样声明的:
void *thr_func(void *arg){
thread_data_t *data = (thread_data_t *)arg;
...
}
thr_func 是一个函数指针,所以我通常使用一个函数指针来通过 & 引用一个现有的、已实现的函数,例如:
thr_func = &thr_func_impl;
虽然 thr_func 的参数也是取消引用的指针,例如通过 * 来检索它们指向的值。
我不明白的是:
当我创建一个线程时,为什么我只在
pthread_create(...,thr_func,...)中给出函数名称而不是它的地址以便它可以使用,例如:pthread_create(...,&thr_func,...)或者这是由pthread_create()完成的?我必须如何理解这部分:
thread_data_t *data = (thread_data_t *)arg;好的,我想取消引用 thread_data 类型的结构通过thread_data_t *data = ...调用数据。 我不应该这样做吗:thread_data_t *data; data = &arg; /* now * on data ,e.g.: *data == struct-data (dereferencing) gives the struct data and data without * just gives the structs start address */
-> 我无法真正了解里面发生的事情:
void *thr_func(void *arg){
thread_data_t *data = (thread_data_t *)arg;
...
}
如果有人有好的解释,我会很高兴,谢谢!
【问题讨论】:
标签: c multithreading pointers pthreads function-pointers