我有下面的代码来获取核心转储错误。每个C实例创建自己的线程然后运行。我猜静态函数和类参数“count”有问题。当我注释掉打印它的代码时,没有发生故障。在类中使用pthreads时出现分段错误
#include <iostream>
#include <pthread.h>
using namespace std;
class C {
public:
int count;
C(int c_): count(c_){}
public:
void *hello(void)
{
std::cout << "Hello, world!" <<std::endl;
std::cout<<count; // bug here!!!
return 0;
}
static void *hello_helper(void *context)
{
return ((C *)context)->hello();
}
void run() {
pthread_t t;
pthread_create(&t, NULL, &C::hello_helper, NULL);
}
};
int main() {
C c(2);
c.run();
C c2(4);
c2.run();
while(true);
return 0;
}
mascoj你是对的,那个参数是指向一个类实例的指针,我当时马虎不在。 。 – eral