线程指针和函数指针
Pthread and function pointers
我对pthread的工作方式有点困惑-具体来说,我很确定pthread接受一个指向一个函数的指针,该函数以一个void指针作为参数(如果我错了请纠正我),并且我以这种方式声明了我的函数,但我仍然得到一个错误。这是我正在努力的代码:
void eva::OSDAccessibility::_resumeWrapper(void* x)
{
logdbg("Starting Connection.");
_listener->resume();
logdbg("Connected.");
pthread_exit(NULL);
}
void eva::OSDAccessibility::resumeConnection()
{
long t;
_listener->setDelegate(_TD);
pthread_t threads[1];
pthread_create(&threads[0], NULL, &eva::OSDAccessibility::_resumeWrapper, (void *)t);
}
得到的错误是:
No matching function for call to pthread_create.
你不必告诉我如何修复代码(尽管这当然会很感激),我更感兴趣的是为什么这个错误会出现,如果我对pthread的理解是正确的。谢谢!:)
-
函数签名必须为
void * function (void*)
-
如果从c++代码中调用,方法必须是静态的:
class myClass { public: static void * function(void *); }
使用非静态方法的解决方案如下:
class myClass
{
// the interesting function that is not an acceptable parameter of pthread_create
void * function();
public:
// the thread entry point
static void * functionEntryPoint(void *p)
{
((myClass*)p)->function();
}
}
启动线程:
myClass *p = ...;
pthread_create(&tid, NULL, myClass::functionEntryPoint, p);
相关文章:
- 如何正确编写指针函数声明?
- C++常规指针函数或模板
- 如何重新定义 C++ 指针函数?
- C++ 指向其他类函数的指针函数
- 指针到指针函数参数
- 将指向成员的指针函数传递到模板中
- C++ 在 none 常量指针函数中返回一个常量指针
- 如何使用指针函数编写/读取数组
- 是C 中的函数指针函数对象
- 如何从另一个类调用指向成员的指针函数
- 如何声明采用指向成员的指针函数的函数
- 如何构造一个以可变参数指针函数作为成员的类?
- c 通过值或指针函数语法
- 将typedef方法作为指针函数传递
- 从 Main 中的双指针函数打印出指针数组
- strcpy 对指针函数的引用
- 这是否仍然声明一种指针函数的别名?
- 将指向成员的指针函数与 std::shared_ptr 结合使用
- 'Incomplete type' 为标准::函数声明指向成员的指针函数模板参数时出错
- 从注入进程的 DLL 调用函数并更改指针函数的地址