pthread_create - 无效使用非静态成员函数

Tha*_*iJo 3 c++ pthreads

我一直在努力学习如何使用线程,而且我一直在努力创建线程.我正在这样的类构造函数中创建线程...

Beacon::Beacon() {
    pthread_create(&send_thread,NULL, send, NULL);
}
Run Code Online (Sandbox Code Playgroud)

发送功能还没有做任何事情,但这就是它的样子.

void Beacon::send(void *arg){
    //Do stuff
}
Run Code Online (Sandbox Code Playgroud)

每次运行代码时,我都会无效使用非静态成员函数错误.我尝试过使用和发送,但是没有用.我也设置了最后一个NULL参数,但是没有用.我一直在寻找其他示例代码来尝试和模仿它,但似乎没有任何工作.我究竟做错了什么?

Som*_*ude 5

如果你不能使用std::thread我建议你创建一个static成员函数来包装你的实际函数,并this作为参数传递给函数.

就像是

class Beacon
{
    ...

    static void* send_wrapper(void* object)
    {
        reinterpret_cast<Beacon*>(object)->send();
        return 0;
    }
};
Run Code Online (Sandbox Code Playgroud)

然后创建类似的线程

pthread_create(&send_thread, NULL, &Beacon::send_wrapper, this);
Run Code Online (Sandbox Code Playgroud)