-1

I'm trying to understand how pthread_create and pthread_join works. I thought that the third argument of pthread_create only allows functions with one argument of void*. I compiled the code below with gcc and it worked just fine. But why?

void *foo() {
    return 0;
}

int main() {
    pthread_t thread_id;
    int par = 5;
    pthread_create(&thread_id, NULL, foo, &par);
    pthread_join(thread_id, NULL);
    return 0;
}
LS_Dev
  • 3
  • 1
  • Which gcc version ? It should say ` ....no known conversion from 'void *()' to 'void *(*)(void *)' for 3rd argument` – P0W Oct 31 '18 at 08:28

1 Answers1

0

void foo()

means that the function foo can take any number of arguments of unknown type, while

void foo(void *)

means that the function foo takes a argument of type void * That is why the program compiles, as your function can accept any type of arguments, including void *

The argument void * is a void pointer that has no associated data type with it. It can hold address of any type and can be typecasted to any type.

To find the difference between foo() and foo(void *) see here

RishabhHardas
  • 495
  • 1
  • 5
  • 25