Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Pthreads,与pthread\u join混淆(pthread\u t,void**)_C++_C_Pthreads_Pthread Join - Fatal编程技术网

C++ Pthreads,与pthread\u join混淆(pthread\u t,void**)

C++ Pthreads,与pthread\u join混淆(pthread\u t,void**),c++,c,pthreads,pthread-join,C++,C,Pthreads,Pthread Join,我不明白为什么pthread\u join将返回值作为第二个参数void**,而pthread\u exit(给定返回值)将返回值参数设置为void*pthread\u join等待线程结束,pthread_exit的结果值存储到*value_ptr中。如果要忽略结果,可以为值\u ptr传递NULL。这是通过将指针传递到变量来模拟按引用传递的常见C实践。看 pthread_join在成功时返回0作为函数返回值;然后您知道线程已经连接,您可以从*value\u ptr访问该值 void *val

我不明白为什么
pthread\u join
将返回值作为第二个参数
void**
,而
pthread\u exit
(给定返回值)将返回值参数设置为
void*

pthread\u join等待线程结束,pthread_exit的结果值存储到*value_ptr中。如果要忽略结果,可以为值\u ptr传递NULL。这是通过将指针传递到变量来模拟按引用传递的常见C实践。看

pthread_join在成功时返回0作为函数返回值;然后您知道线程已经连接,您可以从*value\u ptr访问该值

void *value = NULL;
if (pthread_join(thread, &value) == 0) {
    // thread has ended, and the exit value is available in
    // the value variable
} 

本质上,
pthread\u join()
希望返回两个信息:

  • 成功/失败的迹象
  • 线程返回的内容(类型为
    void*
在C语言中,函数“返回”两个独立值的典型方式是让函数正常返回其中一个值,并在调用者提供的位置“返回”OFR值,调用者在该位置传递指针。所以
pthread\u join()

  • 将成功/失败作为函数值返回
  • 在调用者提供的位置返回线程的
    void*
    结果,调用者将
    void**
    传递给该位置
注意,在
pthread_join()
的情况下,调用方提供的位置是可选的。如果调用方对该结果不感兴趣,则可以在id中传递NULL。使调用者提供的位置可选是一种常见的习惯用法,但决不是通用的


pthread_exit()
不需要使用
void**
作为参数,因为该参数不是函数的结果。因此它可以直接获取值。

简洁地说:返回值是传递给
pthread\u exit()
的值,但存储返回值的位置是传递给
pthread\u join()的值。