pthread_create 我应该使用 & 还是不使用? (C)

pthread_create should I use & or not? (C)

我正在学习 pthreads,但我遇到了一个问题,根据我问的人不同,我得到了不同的答案。例如:

void *server (void * arg){
   printf("I am running");
   return NULL;
}

int main(int){
   pthread_t thread_server;

   pthread_create(&thread_server, NULL, &server, NULL);

   pthread_join(thread_server, NULL);
   return 0;
}

这是正确的还是我应该这样做?:

void *server (void * arg){
    printf("I am running");
    return NULL;
 }

 int main(int){
     pthread_t thread_server;

     pthread_create(&thread_server, NULL, server, NULL);

     pthread_join(thread_server, NULL);
     return 0;
  }

请注意 pthread_create(&server,或服务器)的区别。两者似乎都有效,但有什么区别呢?

Notice the difference at pthread_create (&server, or server). Both seem to work but then what is the difference?

pthread_create 接受一个指向函数的指针。函数名隐式转换为函数指针,您无需显式获取其地址。换句话说,server&server 在这里做同样的事情——将函数 server 指针传递给 pthread_create.

int main() {
    void f(); // Function.
    void(*p)(); // Function pointer.

    p = &f;
    p = f; // Same effect as above.

    return 0;
}

Notice the difference at pthread_create (&server, or server). Both seem to work but then what is the difference?

没有有效差异。它们是等价的。

此(在 C11 中)的参考是 6.3.2.1 Lvalues, arrays, and function designators, paragraph 4 of the C11 standard:

A function designator is an expression that has function type. Except when it is the operand of the sizeof operator, the_Alignof operator, or the unary & operator, a function designator with type "function returning type" is converted to an expression that has type "pointer to function returning type".

给定函数

void *server (void * arg){
   printf("I am running");
   return NULL;
}

pthread_create(&thread_server, NULL, server, NULL);中的裸server标识符就是这样一个"function designator",所以它被隐式转换为函数指针。 &server 已经有类型 "pointer to function".