【发布时间】:2015-10-16 18:50:59
【问题描述】:
我刚开始研究pthread 的跨平台。但我真的对用于pthread_create() 和pthread_join() 的变量类型感到困惑。请看下面的代码。
// This is just simple code for test, so don't take this variable seriously!
int result;
void* myThreadFunc(void* arg) {
result = *(int*)arg;
// Why not &result, but result???
return (void*)result;
}
int main() {
pthread_t myThread;
int argForThread = 10;
int threadResult = 0;
pthread_create(&myThread, NULL, myThreadFunc, (void*)&argForThread);
// Why (void**), but not (void*)?
pthread_join(myThread, (void**)&threadResult);
return 0;
}
正如我在 cmets 中所写,我不了解那些指针,这对我来说没有意义。
对于第一个,为什么我必须将值类型放在一个指针类型?
对于第二个,为什么我只需要一个局部变量的地址进行双指针转换?
【问题讨论】:
-
请一次一个问题。
-
但我觉得说到底还是一个问题,这个问题全是C++中的POINTER。实际上与函数无关。
-
你想要跨平台的代码?使用 C++'
std::thread! -
@UlrichEckhardt 感谢您提供信息。总有一天会很有帮助,但这次,我应该坚持 C :)
-
你的问题是用 C++ 标记的!那是一种不同的语言,你不应该使用 is 作为标签。
标签: c linux multithreading pointers pthreads