Ich habe den folgenden Code, der Core Dumped Fehler bekommt. Jede C-Instanz erstellt ihren eigenen Thread und wird dann ausgeführt. Ich denke, dass etwas mit der statischen Funktion und dem Klassenargument "count" nicht stimmt. Wenn ich den Code auf Kommentar, dass er druckt, tritt kein Fehler ..Segmentierungsfehler bei der Verwendung von Pthreads in Klasse
#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 du hast Recht, das Argument ist Zeiger auf eine Klasseninstanz, ich war schlampig an diesem Punkt. . – eral