Duplicate possible:
c++ multithreadC++ multithread
J'utilise C++ pour implémenter une classe de fil. Le code est le suivant. J'initialise deux objets, souhaite qu'il démarre deux threads (j'utilise pthread_self() pour regarder l'Id de thread). Mais le résultat montre qu'il n'y a qu'un seul thread à côté du thread principal. Je suis un peu confus ...
class Thread {
public:
int mask;
pthread_t thread;
Thread(int);
void start();
static void * EntryPoint (void *);
void Run();
};
Thread::Thread(int a) {
mask =a;
}
void Thread::Run() {
cout<<"thread begin to run" <<endl;
cout <<" Thread Id is: "<< pthread_self() << endl; // the same thread Id.
}
void * Thread::EntryPoint(void * pthis) {
cout << "entry" <<endl;
Thread *pt = (Thread *) pthis;
pt->Run();
}
void Thread::start() {
pthread_create(&thread, NULL, EntryPoint, (void *)ThreadId);
pthread_join(thread, NULL);
}
int main() {
int input_array[8]={3,1,2,5,6,8,7,4};
Thread t1(1);
Thread t2(2);
t1.start();
t2.start()
}