0

I found out that two lines of code have the ambiguous meaning.

         for (i = 0; i < NTHREADS; i++) {
 #ifndef THREAD_POINTER
                 pthread_create(&thread_id[i], NULL, thread_function, NULL);
 #endif
 #ifdef THREAD_POINTER
                 pthread_create(&thread_id[i], NULL, &thread_function, NULL);
 #endif
         }

Here is the two lines of code. They behave the same action that creates NTHREADS numbers of thread. But the first function defines with the function name, and second function defines with the function name pointer. I thought it must be a function pointer so that the thread could be dynamically generated. These two must be two different function in POSIX library. I still don’t understand how this works. They are totally different from the syntax but with the same function name. As far as I know, the function name are only allowed to be the same in C++, but not in C.

If both function name(thread_function) and function name pointer(&thread_function) are void * type, the second one should be void **. So why the system behave the same.

P.S. This two lines of code were tested on MAC OS X 10.10.3 with clang compiler.

  • [`thread_function == &thread_function`](http://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work#comment651729_840504) – Spikatrix May 10 '15 at 14:41
  • There is implicit conversion from a function to function pointer when no suitable usage of it is requested and there is on for a pointer. Which is mostly the case unless you call a function or take it's address (which however actually gives the same result despite that no implicit conversion is used). – AnArrayOfFunctions May 10 '15 at 14:41
  • @FISOCPP , Which are the cases where function "decay"(function name gets converted into a pointer that points to the function's address) doesn't happen? – Spikatrix May 10 '15 at 14:50
  • Already explained above. But actually - yeah this is kinda more important in 'C++' then in 'C' because there are function references and templates but I think it's good to know anyway. – AnArrayOfFunctions May 10 '15 at 15:06

0 Answers0