What is the correct argument to pthread_create

2019-02-25 17:35发布

问题:

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.