0

I want to create a pthread using pthread_create function and pass the function name as

char *function_name="get_time";

int rc = pthread_create(&thread_arr[threadNum], NULL,
    *function_name, (void *) output_cur_node->data);

Also tried using (void*)&function_name

This doesn't seem to enter the function get_time().

Instead, When I use below

int rc = pthread_create(&thread_arr[threadNum], NULL,
    get_time, (void *) output_cur_node->data);

it works fine.

Please advise as to what is wrong here.

Mauren
  • 1,955
  • 2
  • 18
  • 28
user1429246
  • 323
  • 2
  • 4
  • 9

2 Answers2

3

you need to pass addr of function, but you are trying to pass address of a string

Nikole
  • 4,719
  • 3
  • 16
  • 15
  • How do i solve this? basically, I don't know the function names in advance. Rest of my code reads an xml and obtains function names from there in form of strings. – user1429246 Mar 28 '14 at 20:05
  • 1
    you need to maintain function name to address mapping . see http://stackoverflow.com/questions/15349761/get-called-function-name-as-string for details – Nikole Mar 28 '14 at 20:13
0

The prototype of pthread_create is as follows:

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                   void *(*start_routine) (void *), void *arg);

3rd argument has to be of function pointer which is of type

void *(*start_routine) (void *)

So whenever you are trying to pass the argument by doing this

char *function_name="get_time";
(void*)&function_name

effectively means void* type which does not matches with the above type. Hence compile time error would be reported.

Mantosh Kumar
  • 5,659
  • 3
  • 24
  • 48