How to get function pointer in C?

I have a function in C:

void start_fun()
{
   // do something
}

I want to use pthread_create()to create a stream, and the initial routine start_fun(), without modification void start_fun(), how to get a pointer to start_fun();

+4
source share
2 answers

If you write the name of the function start_funwithout any parameters anywhere in your code, you will get a pointer to this function.

However, pthread_create expects a format function void* func (void*).

If rewriting a function is not a parameter, you will have to write a shell:

void* call_start_fun (void* dummy)
{
  (void)dummy;

  start_fun();

  return 0;
}

then go to call_start_fun in pthread_create:

pthread_create(&thread, NULL, call_start_fun, NULL);
+6
source

, , . , :

pthread_t thread_id;
int result = pthread_create(&thread_id, NULL, start_fun, NULL);

, , , , pthread undefined. :

void *start_fun(void *arg);

NULL, , ​​ , ( ).

+3

Source: https://habr.com/ru/post/1606956/


All Articles