코어 덤프 오류가 발생하는 코드가 있습니다. 각 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