【发布时间】:2015-06-30 07:28:21
【问题描述】:
如何使用 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;
}
【问题讨论】:
-
可以用c++11线程吗?
-
我会看看 c++ 11。但我认为它的支持很棘手,我想知道是否有办法使用
pthread