我在C中有一个功能:
void start_fun()
{
// do something
}
我想用来pthread_create()
创建一个线程,并且启动例程是start_fun()
不修改void start_fun()
而如何获取函数指针的方法start_fun()
;
如果您start_fun
在代码中的任何位置编写了不带任何参数的函数名称,则将获得指向该函数的函数指针。
但是pthread_create期望使用format函数void* func (void*)
。
如果无法重写该函数,则必须编写一个包装器:
void* call_start_fun (void* dummy)
{
(void)dummy;
start_fun();
return 0;
}
然后将call_start_fun传递给pthread_create:
pthread_create(&thread, NULL, call_start_fun, NULL);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句