C++ pthread_在类的成员中创建

C++ pthread_在类的成员中创建,c++,oop,class,pthreads,this,C++,Oop,Class,Pthreads,This,假设我有以下课程: *.h: class MyClass{ void caller(); int threadProcuder(void *args); }; void MyClass::caller() { pthread_t i; pthread_create(&i,NULL,(void*(*)(void*))&MyClass::threadProcedure,(void*)this); } int MyClass::threadProce

假设我有以下课程:

*.h

class MyClass{
    void caller();
    int threadProcuder(void *args);
};
void MyClass::caller()
{
    pthread_t i;
    pthread_create(&i,NULL,(void*(*)(void*))&MyClass::threadProcedure,(void*)this);
}

int MyClass::threadProcedure(void *args) 
{
    cout << "body of thread" << endl;
}
*cpp

class MyClass{
    void caller();
    int threadProcuder(void *args);
};
void MyClass::caller()
{
    pthread_t i;
    pthread_create(&i,NULL,(void*(*)(void*))&MyClass::threadProcedure,(void*)this);
}

int MyClass::threadProcedure(void *args) 
{
    cout << "body of thread" << endl;
}
void MyClass::caller()
{
pthread_t i;
pthread_创建(&i,NULL,(void*(*)(void*))&MyClass::threadProcedure,(void*)this);
}
int MyClass::threadProcedure(void*args)
{

我解决了它,我的格式是正确的,我没有调用我的构造函数。

我解决了它,我的格式是正确的,我没有调用我的构造函数。

编辑:传递给
pthread\u create
的函数必须声明为
extern“C”
。有关这种情况的更好解释,请参阅


您不能像我最初的回答所说的那样使用静态成员函数,尽管它的工作方式可能与其未定义的行为相同。

编辑:传递给
pthread\u create
的函数必须声明为
extern“C”
。有关这种情况的更好解释,请参阅

您不能像我最初的回答所说的那样使用静态成员函数,尽管它可能会因为其未定义的行为而工作。

正确的方法是:

// Must declare the callback is extern "C" 
// As you are calling back from a C library.
// As this is compiles as C it can only call functions that use the C ABI
// There is no guarantee in the C++ standard that static member functions
// use the same ABI as a "C" function.
//
// Certain C++ ABI definitions do make this explicit but using this knowledge
// Renders your code non portable. If you use a C++ static member it will likely
// break in the future and when it does tracking down the problem will be nearly
// imposable.
extern "C" void* threadProcuder(void *args);

class MyClass{
    void caller();
    void* threadProcuder();
};

void MyClass::caller()
{
    // NOTE: This function should NOT be called from the constructor.
    //       You should really wait until the object is fully constructed
    //       before letting a thread run around inside your object
    //       otherwise the thread may will start playing with members that
    //       are not fully constructed.
    pthread_t i;
    pthread_create(&i,NULL,&threadProcedure,this);
}

void* threadProcuder(void *args)
{
    // I use reinterpret_cast<> here to make it stand out.
    // Others prefer static_cast<>. Both are valid and guaranteed to work.
    // Casting a pointer to/from void* is guaranteed by the standard
    MyClass* obj    = reinterpret_cast<MyClass*>(args);
    void*    result = NULL;
    try
    {
        result = obj->threadProcuder();
    }
    catch(...) {}   // you MUST catch all exceptions
                    // Failing to do so is very undefined.
                    // In most pthread_application allowing exceptions to escape
                    // usually (but not always) leads to application termination.
    return result;
}
//必须声明回调为外部“C”
//当您从C库回拨时。
//由于它编译为C,因此只能调用使用C ABI的函数
/在C++标准中没有保证静态成员函数
//使用与“C”函数相同的ABI。
//
//某些C++ ABI定义确实使这一点明确,但使用此知识
//使代码不可移植。如果使用C++静态成员,它可能会使用。
//在未来,当它真的发现问题时,追踪问题将是几乎不可能的
//不可能。
外部“C”void*线程处理器(void*args);
类MyClass{
void caller();
void*threadProcuder();
};
void MyClass::caller()
{
//注意:不应从构造函数调用此函数。
//您真的应该等到对象完全构造完毕
//在让线程在对象内部运行之前
//否则线程可能会开始与
//未完全构建。
pthread_t i;
pthread_创建(&i,NULL,&threadProcedure,this);
}
void*threadProcuder(void*args)
{
//我在这里使用reinterpret_cast使其脱颖而出。
//其他人更喜欢静态_cast。两者都是有效的,并保证工作。
//本标准保证向void*或从void*投射指针
MyClass*obj=重新解释铸件(参数);
void*result=NULL;
尝试
{
结果=obj->threadProcuder();
}
catch(…){}//您必须捕获所有异常
//不这样做是非常不明确的。
//在大多数pthread_应用程序中,允许异常转义
//通常(但并非总是)会导致应用程序终止。
返回结果;
}
正确的方法是:

// Must declare the callback is extern "C" 
// As you are calling back from a C library.
// As this is compiles as C it can only call functions that use the C ABI
// There is no guarantee in the C++ standard that static member functions
// use the same ABI as a "C" function.
//
// Certain C++ ABI definitions do make this explicit but using this knowledge
// Renders your code non portable. If you use a C++ static member it will likely
// break in the future and when it does tracking down the problem will be nearly
// imposable.
extern "C" void* threadProcuder(void *args);

class MyClass{
    void caller();
    void* threadProcuder();
};

void MyClass::caller()
{
    // NOTE: This function should NOT be called from the constructor.
    //       You should really wait until the object is fully constructed
    //       before letting a thread run around inside your object
    //       otherwise the thread may will start playing with members that
    //       are not fully constructed.
    pthread_t i;
    pthread_create(&i,NULL,&threadProcedure,this);
}

void* threadProcuder(void *args)
{
    // I use reinterpret_cast<> here to make it stand out.
    // Others prefer static_cast<>. Both are valid and guaranteed to work.
    // Casting a pointer to/from void* is guaranteed by the standard
    MyClass* obj    = reinterpret_cast<MyClass*>(args);
    void*    result = NULL;
    try
    {
        result = obj->threadProcuder();
    }
    catch(...) {}   // you MUST catch all exceptions
                    // Failing to do so is very undefined.
                    // In most pthread_application allowing exceptions to escape
                    // usually (but not always) leads to application termination.
    return result;
}
//必须声明回调为外部“C”
//当您从C库回拨时。
//由于它编译为C,因此只能调用使用C ABI的函数
/在C++标准中没有保证静态成员函数
//使用与“C”函数相同的ABI。
//
//某些C++ ABI定义确实使这一点明确,但使用此知识
//使代码不可移植。如果使用C++静态成员,它可能会使用。
//在未来,当它真的发现问题时,追踪问题将是几乎不可能的
//不可能。
外部“C”void*线程处理器(void*args);
类MyClass{
void caller();
void*threadProcuder();
};
void MyClass::caller()
{
//注意:不应从构造函数调用此函数。
//您真的应该等到对象完全构造完毕
//在让线程在对象内部运行之前
//否则线程可能会开始与
//未完全构建。
pthread_t i;
pthread_创建(&i,NULL,&threadProcedure,this);
}
void*threadProcuder(void*args)
{
//我在这里使用reinterpret_cast使其脱颖而出。
//其他人更喜欢静态_cast。两者都是有效的,并保证工作。
//本标准保证向void*或从void*投射指针
MyClass*obj=重新解释铸件(参数);
void*result=NULL;
尝试
{
结果=obj->threadProcuder();
}
catch(…){}//您必须捕获所有异常
//不这样做是非常不明确的。
//在大多数pthread_应用程序中,允许异常转义
//通常(但并非总是)会导致应用程序终止。
返回结果;
}

检查
pthread\u create
返回的错误代码。您从未声明过
MyClass::threadProcedure
。代码无法编译,也没有意义。可能重复:@moooeeep:不幸的是,在那个问题中,排名靠前的答案是错误的。@LokiAstari我知道,我在该答案下面留下了一条评论。确实,不幸的是。检查错误c由
pthread\u create
返回的ode,您从未声明过
MyClass::threadProcedure
。代码不可编译,也没有意义。可能重复:@moooeeep:不幸的是,在这个问题上,排名靠前的答案是错误的。@LokiAstari我知道,我在这个答案下面留下了一条评论。确实,不幸的是。
静态的
是不够的ent.
pthread\u create
是一个C函数,它接受指向C函数的指针。传递给它的函数必须是
extern“C”
,因此它不能成为成员,即使是
静态的
。你不能使用静态成员函数作为pthread\u create的目标。如果它正好工作,你只是运气好,而且它是不可移植的。请看,我想我错过了。我会在一秒钟内编辑我的答案。对于那些想知道的人,这里有一个更好的解释:
静态的
Isn不够。
pthread\u create
是一个C函数,并获取指向C函数的指针。传递给它的函数必须是
extern“C”
,因此它不能是成员,甚至