What is the correct argument to pthread_create

跟風遠走 提交于 2019-12-02 03:08:11

问题


I have seen the documentation of pthread_create

In the example at the bottom they are using:

pthread_create(&tinfo[tnum].thread_id, &attr, &thread_start, &tinfo[tnum]);

&thread_start - with &

but in other examples I have seen online they were not using the &:

pthread_create(&tinfo[tnum].thread_id, &attr, thread_start, &tinfo[tnum]);

I have also tested and it works without &.

But which is the correct way?


回答1:


Short answer: both are correct.


The signature of pthread_create is:

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

So start_routine is a function pointer that takes a void * argument and returns void *.

Back to your question, I assume thread_start is the name of the function, so &thread_start is a function pointer which is correct.

However, thread_start is correct, too, because the function name is automatically converted to a function pointer.



来源:https://stackoverflow.com/questions/28148395/what-is-the-correct-argument-to-pthread-create

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!