C ++:使用pthread_create创建新线程,以运行类成员函数 [英] C++: Creating new thread using pthread_create, to run a class member function

查看:1076
本文介绍了C ++:使用pthread_create创建新线程,以运行类成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

class A
{
    private:
        int starter()
        {
             //TO_DO: pthread_create()
        }

        void* threadStartRoutine( void *pThis );
}



我希望从starter()中创建一个线程来运行threadStartRoutine 。我得到关于第三个参数的编译时错误,它应该包含启动例程的地址。

I want to create a thread from inside starter() to run threadStartRoutine(). I get compile time errors with regard to the third argument, which should take the address of the start routine.

调用pthread_create()创建的正确方法是什么一个新的线程,开始执行threadStartRoutine()?

What would be the correct way to call pthread_create() to create a new thread that starts executing threadStartRoutine() ?

我在网上找到的文章说,大多数编译器不允许非静态成员函数使用pthread_create )。这是真的?这是什么原因?

I have come across articles online that say that most compilers do not allow non-static member functions to be invoked using pthread_create(). Is this true? What is the reason behind this?

我使用G ++在Linux-x64上编译我的程序。

I am compiling my program on Linux-x64 using G++.

推荐答案

threadStartRountine()声明为 static

static void* threadStartRoutine( void *pThis );


$ b <是:

Otherwise, the type of threadStartRoutine() is:

void* (A::*)(void*)

这不是 pthread_create()所需的函数指针类型。

which is not the type of function pointer that pthread_create() requires.

这篇关于C ++:使用pthread_create创建新线程,以运行类成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆