我看过的文档线程创建
在底部的示例中,他们使用:
pthread_create(&tinfo[tnum].thread_id, &attr, &thread_start, &tinfo[tnum]);
&thread_start
- with &
但在我在网上看到的其他例子中,他们没有使用&
:
pthread_create(&tinfo[tnum].thread_id, &attr, thread_start, &tinfo[tnum]);
我也测试过,它可以在没有&
.
但哪个是正确的方法呢?
简短回答:两者都是正确的。
的签名pthread_create
is:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
So start_routine
是一个函数指针,它接受一个void *
参数和返回值void *
.
回到你的问题,我假设thread_start
是函数的名称,所以&thread_start
是一个函数指针,这是正确的。
然而,thread_start
也是正确的,因为函数名称会自动转换为函数指针。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)