为什么为的start_routine返回pthread_create的无效*,并采取无效* [英] Why does start_routine for pthread_create return void* and take void*

查看:128
本文介绍了为什么为的start_routine返回pthread_create的无效*,并采取无效*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在pthread_create 函数头如下:

int pthread_create(pthread_t * thread, 
                   const pthread_attr_t * attr,
                   void * (*start_routine)(void *), 
                   void *arg);

我明白这一切只是为的start_routine 函数指针的形式无效*(* fpointer)(无效*)的,这意味着它需要在空指针并返回空指针

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 pointer and returns a void pointer.

void指针,它需要只是一种方式,将参数传递给的start_routine,我得到的一部分,但我不明白为什么函数返回一个空指针?什么code甚至会发现,空指针?

The void pointer that it takes 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 pointer? What code will even notice that void pointer?

推荐答案

从<一个文档href=\"http://www.opengroup.org/onlinepubs/007908799/xsh/pthread%5Fcreate.html\">pthread_create:

在创建线程与ARG作为其唯一的参数执行的start_routine。如果start_routine返回时,效果好像有到了pthread_exit()使用的start_routine作为退出状态的返回值隐式调用。注意,螺纹在其中主()最初调用不同于此。当它从主()返回时,效果就好像有一个隐式调用使用的主要的返回值()作为退出状态退出()。

The thread is created executing start_routine with arg as its sole argument. 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. Note that the thread in which main() was originally invoked differs from this. 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.

了pthread_exit

在了pthread_exit()函数可用来终止调用线程并使其可用于任何成功的价值value_ptr与终止线程加入。

The pthread_exit() function terminates the calling thread and makes the value value_ptr available to any successful join with the terminating thread.

所以,如果你在一个线程做一个在pthread_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.

这篇关于为什么为的start_routine返回pthread_create的无效*,并采取无效*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆