pthread_create - 无效使用非静态成员函数
pthread_create - invalid use of non-static member function
我一直在尝试学习如何使用线程,但我陷入了创建线程的困境。我正在这样的类构造函数中创建线程......
Beacon::Beacon() {
pthread_create(&send_thread,NULL, send, NULL);
}
send 函数尚未执行任何操作,但如下所示。
void Beacon::send(void *arg){
//Do stuff
}
每次我运行代码时,我都会收到无效使用非静态成员功能错误。我试过使用&发送,但没有用。我还为此设置了最后一个 NULL 参数,但这不起作用。我一直在查看其他示例代码来尝试模仿它,但似乎没有任何效果。我做错了什么?
如果你不能使用std::thread
我建议你创建一个static
成员函数来包装你的实际函数,并将this
作为参数传递给函数。
类似的东西
class Beacon
{
...
static void* send_wrapper(void* object)
{
reinterpret_cast<Beacon*>(object)->send();
return 0;
}
};
然后创建线程,例如
pthread_create(&send_thread, NULL, &Beacon::send_wrapper, this);
相关文章:
- 如何在C++中使用非静态成员函数作为回调函数
- 静态成员函数使用相同的名称时出现模板类型名称错误
- 在 nullptr 上调用无状态类的非静态成员函数是否合法?
- 为什么传递非静态成员函数会导致编译错误?
- 处理类内的回调时,必须调用对非静态成员函数的引用
- 非静态成员函数的 decltype 格式不正确吗?
- std::异步与非静态成员函数
- C++无效使用非静态成员函数?
- 未使用的C++未优化的静态成员函数/变量
- C++:如何返回指向非静态成员函数的指针?
- 使用静态成员函数而不是普通函数是否有任何开销?
- 在 stream_descriptor::async_wait 中无效使用非静态成员函数
- 为什么我不能像这样在静态成员函数中调用静态成员变量?
- 无法在clang Linux中分配非静态成员函数,但我可以在Visual Studio Windows中分配
- 在类定义中推导内联静态成员函数的返回类型
- `enable_if()`以禁用模板化类的静态成员函数声明
- 错误:无效使用非静态成员函数"int test::hotplug_callback(libusb_contex
- 常量静态成员函数
- 使用 vector.sort() 的非静态成员函数无效使用
- 从静态成员函数访问私有非静态类变量 - C++