[英]Why does start_routine for pthread_create return void* and take void*
The function header for pthread_create
looks like this: pthread_create
的函数头如下所示:
int pthread_create(pthread_t * thread,
const pthread_attr_t * attr,
void * (*start_routine)(void *),
void *arg);
I understand it all except that the function pointer for start_routine
is of the form void* (*fpointer) (void*)
which means it takes in a void*
and returns a void*
.我理解这一切,除了
start_routine
的函数指针的形式为void* (*fpointer) (void*)
这意味着它接受一个void*
并返回一个void*
。
The void*
parameter is just a way to pass in an argument to the start_routine
, I get that part, but I don't understand why the function returns a void*
? void*
参数只是将参数传递给start_routine
一种方式,我得到了那部分,但我不明白为什么该函数返回void*
? What code will even notice that return value?什么代码甚至会注意到返回值?
From the documentation for pthread_create
:从
pthread_create
的文档中:
The thread is created executing
start_routine
witharg
as its sole argument.线程是在执行
start_routine
时创建的,arg
作为其唯一参数。 If thestart_routine
returns, the effect is as if there was an implicit call topthread_exit()
using the return value ofstart_routine
as the exit status.如果
start_routine
返回,效果就像使用start_routine
的返回值作为退出状态对pthread_exit()
进行了隐式调用。 Note that the thread in whichmain()
was originally invoked differs from this.请注意,最初调用
main()
的线程与此不同。 When it returns frommain()
, the effect is as if there was an implicit call toexit()
using the return value ofmain()
as the exit status.当它从
main()
返回时,效果就像使用main()
的返回值作为退出状态对exit()
进行了隐式调用。
And pthread_exit
:和
pthread_exit
:
The
pthread_exit()
function terminates the calling thread and makes the valuevalue_ptr
available to any successfuljoin
with the terminating thread.pthread_exit()
函数终止调用线程并使值value_ptr
可用于与终止线程的任何成功join
。
So if you do a pthread_join
on a thread, the pointer it returns is passed back to the joining thread, allowing you to transmit information from the dying thread to another, living thread.因此,如果您在线程上执行
pthread_join
,则它返回的指针将传递回加入线程,从而允许您将信息从死亡线程传输到另一个活着的线程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.