【问题标题】:What is the correct argument to pthread_createpthread_create 的正确参数是什么
【发布时间】:2015-01-26 10:29:33
【问题描述】:

我看过pthread_create的文档

在他们使用的底部示例中:

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

&thread_start - 与&

但在我在网上看到的其他示例中,他们没有使用&

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

我也进行了测试,没有& 也可以工作。

但是哪种方法是正确的呢?

【问题讨论】:

    标签: c pthreads function-pointers


    【解决方案1】:

    简短回答:两者都是正确的。


    pthread_create的签名是:

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

    所以start_routine 是一个函数指针,它接受void * 参数并返回void *

    回到你的问题,我假设thread_start 是函数的名称,所以&thread_start 是一个正确的函数指针。

    不过,thread_start 也是正确的,因为函数名会自动转换为函数指针。

    【讨论】:

      猜你喜欢
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 2021-01-05
      • 2011-04-16
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      相关资源
      最近更新 更多