简体   繁体   English

将函数名称作为字符指针传递给C中的pthread_create

[英]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 我想使用pthread_create函数创建一个pthread并将函数名称传递为

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 也尝试使用(void*)&function_name

This doesn't seem to enter the function get_time() . 这似乎没有输入函数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: pthread_create的原型如下:

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 *) 无效*(* start_routine)(无效*)

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. 有效地表示与上述类型不匹配的void *类型。 Hence compile time error would be reported. 因此,将报告编译时错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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