简体   繁体   中英

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.

您需要传递函数的addr,但您正在尝试传递字符串的地址

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM