S.
E TP
Threads Java
import java. awt.*;
class Land extends Thread
{ public void run( )
{ System. out. println("mein thread Land");
try { Thread. sleep( 1000); }
catch (InterruptedException e){}
System. out. println(" Freud thread Land");
public class Principal
{ public static void main( String args[ ])
{ Land t = new Land();
t. start();
System. out. println(" je me nomme thread principal ");
try { Thread. sleep( 1000); }
catch (InterruptedException e) {}
System. out. Println (" Je me nomme le thread principal");
3GI ENSP 1
S.E TP
Threads POSIX
// exemple_threads.c
#define _REENTRANT
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
void afficher(int n, char lettre)
{ int i,j;
for (j=1; j<n; j++)
{ for (i=1; i < 10000000; i++);
printf("%c",lettre);
fflush(stdout);
void *threadA(void *inutilise)
{ afficher(100,'A');
printf("\n Fin du thread A\n");
fflush(stdout);
pthread_exit(NULL);
void *threadC(void *inutilise)
afficher(150,'C');
printf("\n Fin du thread C\n");
3GI ENSP 2
S.E TP
fflush(stdout);
pthread_exit(NULL);
void *threadB(void *inutilise)
pthread_t thC;
pthread_create(&thC, NULL, threadC, NULL);
afficher(100,'B');
printf("\n Le thread B attend la fin du thread C\n");
pthread_join(thC,NULL);
printf("\n Fin du thread B\n");
fflush(stdout);
pthread_exit(NULL);
int main()
int i;
pthread_t thA, thB;
printf("Creation du thread A");
pthread_create(&thA, NULL, threadA, NULL);
pthread_create(&thB, NULL, threadB, NULL);
sleep(1);
//attendre que les threads aient termine
printf("Le thread principal attend que les autres se terminent\n");
pthread_join(thA,NULL);
pthread_join(thB,NULL);
exit(0);
3GI ENSP 3
S.E TP
Partage de variables
// programme threads.c
#include <unistd.h> //pour sleep
#include <pthread.h>
#include <stdio.h>
int glob=0;
void* decrement(void * x)
int dec=1;
sleep(1);
glob = glob - dec ;
printf("ici decrement[%d], glob = %d\n",pthread_self(),glob);
pthread_exit(NULL);
void* increment (void * x)
int inc=2;
sleep(10);
glob = glob + inc;
printf("ici increment[%d], glob = %d\n",pthread_self(), glob);
pthread_exit(NULL);
3GI ENSP 4
S.E TP
int main( )
pthread_t tid1, tid2;
printf("ici main[%d], glob = %d\n", getpid(),glob);
//creation d'un thread pour increment
if ( pthread_create(&tid1, NULL, increment, NULL) != 0)
return -1;
printf("ici main: creation du thread[%d] avec succes\n",tid1);
// creation d'un thread pour decrement
if ( pthread_create(&tid2, NULL, decrement, NULL) != 0)
return -1;
printf("ici main: creation du thread [%d] avec succes\n",tid2);
// attendre la fin des threads
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
printf("ici main : fin des threads, glob = %d \n",glob);
return 0;
Passage de paramètres à un thread
// Programme test.cpp
#define _REENTRANT
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <pthread.h>
#include <sys/types.h>
3GI ENSP 5
S.E TP
#include <sys/time.h>
#include <unistd.h>
using namespace std;
int MAX=5;
inline int gen_alea( int, int );
void *afficher( void * );
int main(int argc, char *argv[ ])
pthread_t thread_id[MAX];
int retour;
setvbuf(stdout, (char *) NULL, _IONBF, 0);
if ( argc > MAX+1 ){ // verifier la liste d'arguments
cerr << *argv << " arg1, arg2, ... arg" << MAX << endl;
return 1;
cout << "Affichage" << endl;
for (int i = 0; i < argc-1; ++i)
{ // creation des threads
if( pthread_create(&thread_id[i],NULL,afficher, (void *)argv[i+1]) > 0) {
cerr << "Echec a la creation des threads" << endl;
return 2;
for (int i=0; i < argc-1; ++i)
{ // attendre les threads
if ( pthread_join(thread_id[i], (void **) retour) > 0){
cerr << "Echec a l'attente des threads" << endl;
3GI ENSP 6
S.E TP
return 3;
cout << endl << "Thread " << thread_id[i] << " retourne " << retour;
cout << endl << "Termine" << endl;
return 0;
// fin de main( )
// Afficher, un nombre aléatoire de fois, un mot
void * afficher(void *mot)
int nombre = gen_alea(2,6);
cout << (char *)mot << "\t sera affiche " << nombre << " fois." << endl;
for (int i=0; i < nombre; ++i){
sleep(1);
cout << (char *) mot << " ";
return (void *) nombre;
3GI ENSP 7