简体   繁体   English

为什么 pthread_create 的 start_routine 返回 void* 并取 void*

[英]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 with arg as its sole argument.线程是在执行start_routine时创建的, arg作为其唯一参数。 If the start_routine returns, the effect is as if there was an implicit call to pthread_exit() using the return value of start_routine as the exit status.如果start_routine返回,效果就像使用start_routine的返回值作为退出状态对pthread_exit()进行了隐式调用。 Note that the thread in which main() was originally invoked differs from this.请注意,最初调用main()的线程与此不同。 When it returns from main() , the effect is as if there was an implicit call to exit() using the return value of main() as the exit status.当它从main()返回时,效果就像使用main()的返回值作为退出状态对exit()进行了隐式调用。

And pthread_exit :pthread_exit

The pthread_exit() function terminates the calling thread and makes the value value_ptr available to any successful join 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 ,则它返回的指针将传递回加入线程,从而允许您将信息从死亡线程传输到另一个活着的线程。

From the spec :规范

If the start_routine returns, the effect is as if there was an implicit call to pthread_exit() using the return value of start_routine as the exit status.如果start_routine返回,效果就像使用start_routine的返回值作为退出状态对pthread_exit()进行了隐式调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM