user1429246
user1429246

Reputation: 343

Pass function name as character pointer to pthread_create in C

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.

Upvotes: 0

Views: 246

Answers (2)

Mantosh Kumar
Mantosh Kumar

Reputation: 5741

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.

Upvotes: 0

Nikole
Nikole

Reputation: 4749

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

Upvotes: 3

Related Questions