1

I am learning about pthreads but I got one question where I got different answers depending on whom I ask. For example:

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;
}

is this correct or should I do it like this?:

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;
  }

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

J. Doe
  • 335
  • 1
  • 2
  • 11
  • Maybe this is helpful: https://stackoverflow.com/questions/9552663/function-pointers-and-address-of-a-function – dragosht Jan 29 '20 at 10:09
  • As long as there are not two consecutive unary `&` address operators, then it makes no difference what sequence of zero or more unary `&` address and unary `*` indirection operators you apply to the function name, the result will always be a pointer to the function. The rules in C11 6.3.2.1p4, 6.5.3.2p3 and 6.5.3.2p4 ensure it. – Ian Abbott Jan 29 '20 at 14:44
  • regarding: `void *server (void * arg){` This will cause a compiler warning due to the unused parameter, To correct that warning, the first line in the body of the function should be: `(void)arg;` – user3629249 Jan 30 '20 at 08:45
  • in function: `server()` regarding; `return NULL;` since this is a thread function, a much clearer return statement would be: `pthread_exit( NULL );` – user3629249 Jan 30 '20 at 08:47
  • regarding the statement: `int main(int){` This is not a valid signature for main()`! Suggest using one of the two valid signatures: `int main( void )` -or- `int main( int argc, char *argv[] )` – user3629249 Jan 30 '20 at 08:48
  • regarding the statement: `pthread_t thread_server;` a much better variable name would be: `pthread_t threadID;` – user3629249 Jan 30 '20 at 08:50
  • regarding: `printf("I am running");` This will output the text to the `stdout` buffer. To get that text to be immediately output to the terminal, end the format literal with '\n'. I.E. `printf("I am running\n");` – user3629249 Jan 30 '20 at 08:52
  • regarding the calls to `pthread_create()` and `pthread_join()` They return an indication of 'success' ==0 or 'failure' != 0. The code should always check the returned value from C library functions to assure the operation was successful. If not successful, call `perror( "my error msg" );` – user3629249 Jan 30 '20 at 08:54

2 Answers2

3

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

pthread_create takes a pointer to a function. Function name implicitly converts to a function pointer, you don't need to take its address explicitly. In other words, both server and &server do the same thing here - pass function server pointer to pthread_create.

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

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

    return 0;
}
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
0

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

There is no effective difference. They are equivalent.

The reference for this (in C11) is 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".

Given the function

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

The bare server identifier in pthread_create(&thread_server, NULL, server, NULL); is such a "function designator", so it is implicitly converted to a function pointer. &server already has the type "pointer to function".

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56