salut,
J'ai récupéré un cours de C sur internet.
Il y a différents exemples de codes mais souvent je dois les adapter car tels quels ils ne compilent pas !
Je suis sous ubuntu et j'utilise eclipse (plugin CDT) et je compile avec GCC 4.1.3
Voici un exemple :
code original qui ne compile pas chez moi
code que j'ai adapté :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <stdio.h> #include <pthread.h> pthread_t pthread_id[3]; void f_thread(int i){ printf("je la %d-eme thread d'identité %d.%d\n",i,getpid(),pthread_self()); } main(int argc, char **argv) { int i; for(i=0;i<3;i++) if (pthread_create(pthread_id+i,pthread_attr_default,f_thread,i) == -1) fprintf(stderr,"erreur de creation du pthread n° %d\n",i); printf("je suis la thread initiale %d.%d\n",getpid(),pthread_self()); pthread_join(); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 #include <stdio.h> #include <pthread.h> // import de unistd.h pour getpid(); #include <unistd.h> pthread_t pthread_id[3]; void f_thread(int* i){ // cast sur pthread_self() sinon "error %d expects type int but has type pthread_t" printf("je la %d-eme thread d'identité %d.%d\n",*i,getpid(),(int)pthread_self()); } int main(int argc, char **argv) { int i; for(i=0;i<3;i++){ // argument 4 de pthread_create : un pointeur sinon error makes pointer from interger without a cast // cast sur argument 3 sinon error incompatible pointer type // pthread_attr_default est inconnu if (pthread_create(pthread_id+i,NULL,(void*)f_thread,&i) == -1) fprintf(stderr,"erreur de creation du pthread n° %d\n",i); } // cast sur pthread_self() printf("je suis la thread initiale %d.%d\n",getpid(),(int)pthread_self()); for(i=0;i<3;i++) //pthread_join() nécessite deux paramètres pthread_join(pthread_id[i],NULL); return 0; }
Est ce que cela peut venir d'un problème de norme?
merci de vos éclairages
Partager