如何使用pthread将thread_ready_function
调用为注释的线程?我需要用类对象调用它(在现实世界中,函数使用先前设置的属性)。
MWE
#include <iostream>
#include <pthread.h>
class ClassA
{
public:
void * thread_ready_function(void *arg)
{
std::cout<<"From the thread"<<std::endl;
pthread_exit((void*)NULL);
}
};
class ClassB
{
ClassA *my_A_object;
public:
void test(){
my_A_object = new ClassA();
my_A_object->thread_ready_function(NULL);
// my_A_object->thread_ready_function(NULL);
// ^
// I want to make that call into a thread.
/* Thread */
/*
pthread_t th;
void * th_rtn_val;
pthread_create(&th, NULL, my_A_object.thread_ready_function, NULL);
pthread_join(th, &th_rtn_val);
*/
}
};
int main()
{
ClassB *my_B_object = new ClassB();
my_B_object->test();
return 0;
}
答案 0 :(得分:0)
如果您不想使用C ++ 11或stl或boost,则必须使用 static 关键字作为成员函数,以便pthread可以调用您的成员函数! 示例代码:
Child