//fichier tst-thread.cpp , Patrick TRAU - IPST ULP //pour un petit tuto : http://pficheux.free.fr/articles/lmf/threads/ // ou http://www.estvideo.com/dew/index/page/programmation-systeme-linux #include <iostream.h> #include <stdlib.h> #include <pthread.h> int glob; void *mon_processus (void * arg) { int i,j; for (i = 0 ; i < 5 ; i++) { glob++; cout<<"Thread "<< (char*)arg<<" i= "<<i<<" glob= "<<glob<<"\n"; sleep (1*atoi((char*)arg)); } pthread_exit (0); } main (void) { pthread_t th1, th2; void *ret; glob=1; if (pthread_create (&th1, NULL, mon_processus, (void*)"1") < 0) { cout<<"erreur pthread_create thread 1\n"; exit (1); } if (pthread_create (&th2, NULL, mon_processus, (void*)"2") < 0) { cout<<"erreur pthread_create thread 2\n"; exit (1); } pthread_join (th1, &ret); pthread_join (th2, &ret); }
Patrick
TRAU, ULP - IPST
nov 05