[英]What is the type of "start_routine" passed to pthread_create
[英]Why does start_routine for pthread_create return void* and take void*
pthread_create
的函数头如下所示:
int pthread_create(pthread_t * thread,
const pthread_attr_t * attr,
void * (*start_routine)(void *),
void *arg);
我理解这一切,除了start_routine
的函数指针的形式为void* (*fpointer) (void*)
这意味着它接受一个void*
并返回一个void*
。
void*
参数只是将参数传递给start_routine
一种方式,我得到了那部分,但我不明白为什么该函数返回void*
? 什么代码甚至会注意到返回值?
从pthread_create
的文档中:
线程是在执行
start_routine
时创建的,arg
作为其唯一参数。 如果start_routine
返回,效果就像使用start_routine
的返回值作为退出状态对pthread_exit()
进行了隐式调用。 请注意,最初调用main()
的线程与此不同。 当它从main()
返回时,效果就像使用main()
的返回值作为退出状态对exit()
进行了隐式调用。
和pthread_exit
:
pthread_exit()
函数终止调用线程并使值value_ptr
可用于与终止线程的任何成功join
。
因此,如果您在线程上执行pthread_join
,则它返回的指针将传递回加入线程,从而允许您将信息从死亡线程传输到另一个活着的线程。
从规范:
如果
start_routine
返回,效果就像使用start_routine
的返回值作为退出状态对pthread_exit()
进行了隐式调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.