+ All Categories
Home > Documents > Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple...

Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple...

Date post: 07-Sep-2020
Category:
Upload: others
View: 6 times
Download: 0 times
Share this document with a friend
59
Introduction à la programmation concurrente Introduction Yann Thoma Reconfigurable and Embedded Digital Systems Institute Haute Ecole d’Ingénierie et de Gestion du Canton de Vaud This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License Février 2017 Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 1 / 42
Transcript
Page 1: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Introduction à la programmation concurrenteIntroduction

Yann Thoma

Reconfigurable and Embedded Digital Systems InstituteHaute Ecole d’Ingénierie et de Gestion du Canton de Vaud

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

Février 2017

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 1 / 42

Page 2: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Plan

1 Introduction

2 Thread

3 Exemple

4 Implémentation

5 Séparation d’un programme en plusieurs threads

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 2 / 42

Page 3: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Introduction

Programmation standard

DéfinitionUn processus est une entité active et exécutableou

Un processus est un programme en cours d’exécution

Un programme exécute des instructions séquentiellement

Le développeur maîtrise la suite des opérations

L’exécution est prévisible

L’exécution est reproductible

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 3 / 42

Page 4: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Introduction

Qu’est-ce que la programmation concurrente?

La programmation concurrente est un paradigme de programmationtenant compte, dans un programme, de plusieurs contextes d’exécution(threads, processus, tâches) matérialisés par une pile d’exécution (stack)et des données privées

La concurrence est indispensable lorsque l’on souhaite écrire desprogrammes interagissant avec le monde réel ou tirant parti de multiplesprocesseurs (multi-coeurs, clusters, cloud, ...)

Un processus est décomposé en threads

Un thread est une sorte de processus légerUn thread correspond à une tâche qui s’exécute

Plus ou moins indépendamment des autres

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 4 / 42

Page 5: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Introduction

Pourquoi la programmation concurrente?

Optimiser l’utilisation du/des processeurs

Eviter de bloquer sur des entrées/sorties (IO)Tirer avantage des architectures multi-cores :

Aujourd’hui n’importe quel PC possède au moins 2 coeursAugmenter le parallélisme :

Tout programme faisant du calcul devrait être développé de manièreconcurrente

Attendre des événements de plusieurs entrées

Simplifier la structure d’un programmeSatisfaire des contraintes temporelles :

Programmation temps réel

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 5 / 42

Page 6: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Introduction

Exemple d’applications multi-thread

Serveur web ou ftp : chaque client est géré par un thread

Browser : un thread gère une connexion, un thread fait le rendu de lapage, un thread décode une vidéo, un thread gère l’interaction avecl’interface (tout ceci pour un seul onglet)

Jeu vidéo : un thread s’occupe du rendu graphique, un autre de l’IA, unautre du son, n threads gèrent n joypads, etc.

Viewer d’images : un thread affiche l’image courante alors que n autresthreads chargent les n images suivantes

Programme de retouche d’image : image divisée en n blocs, n threadsfaisant le rendu de 1 bloc

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 6 / 42

Page 7: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Introduction

Exemple d’applications multi-thread

Serveur web ou ftp : chaque client est géré par un thread

Browser : un thread gère une connexion, un thread fait le rendu de lapage, un thread décode une vidéo, un thread gère l’interaction avecl’interface (tout ceci pour un seul onglet)

Jeu vidéo : un thread s’occupe du rendu graphique, un autre de l’IA, unautre du son, n threads gèrent n joypads, etc.

Viewer d’images : un thread affiche l’image courante alors que n autresthreads chargent les n images suivantes

Programme de retouche d’image : image divisée en n blocs, n threadsfaisant le rendu de 1 bloc

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 6 / 42

Page 8: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Introduction

Exemple d’applications multi-thread

Serveur web ou ftp : chaque client est géré par un thread

Browser : un thread gère une connexion, un thread fait le rendu de lapage, un thread décode une vidéo, un thread gère l’interaction avecl’interface (tout ceci pour un seul onglet)

Jeu vidéo : un thread s’occupe du rendu graphique, un autre de l’IA, unautre du son, n threads gèrent n joypads, etc.

Viewer d’images : un thread affiche l’image courante alors que n autresthreads chargent les n images suivantes

Programme de retouche d’image : image divisée en n blocs, n threadsfaisant le rendu de 1 bloc

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 6 / 42

Page 9: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Introduction

Exemple d’applications multi-thread

Serveur web ou ftp : chaque client est géré par un thread

Browser : un thread gère une connexion, un thread fait le rendu de lapage, un thread décode une vidéo, un thread gère l’interaction avecl’interface (tout ceci pour un seul onglet)

Jeu vidéo : un thread s’occupe du rendu graphique, un autre de l’IA, unautre du son, n threads gèrent n joypads, etc.

Viewer d’images : un thread affiche l’image courante alors que n autresthreads chargent les n images suivantes

Programme de retouche d’image : image divisée en n blocs, n threadsfaisant le rendu de 1 bloc

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 6 / 42

Page 10: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Introduction

Exemple d’applications multi-thread

Serveur web ou ftp : chaque client est géré par un thread

Browser : un thread gère une connexion, un thread fait le rendu de lapage, un thread décode une vidéo, un thread gère l’interaction avecl’interface (tout ceci pour un seul onglet)

Jeu vidéo : un thread s’occupe du rendu graphique, un autre de l’IA, unautre du son, n threads gèrent n joypads, etc.

Viewer d’images : un thread affiche l’image courante alors que n autresthreads chargent les n images suivantes

Programme de retouche d’image : image divisée en n blocs, n threadsfaisant le rendu de 1 bloc

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 6 / 42

Page 11: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Introduction

Exemple d’applications multi-thread

Serveur web ou ftp : chaque client est géré par un thread

Browser : un thread gère une connexion, un thread fait le rendu de lapage, un thread décode une vidéo, un thread gère l’interaction avecl’interface (tout ceci pour un seul onglet)

Jeu vidéo : un thread s’occupe du rendu graphique, un autre de l’IA, unautre du son, n threads gèrent n joypads, etc.

Viewer d’images : un thread affiche l’image courante alors que n autresthreads chargent les n images suivantes

Programme de retouche d’image : image divisée en n blocs, n threadsfaisant le rendu de 1 bloc

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 6 / 42

Page 12: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Introduction

Problèmes

Difficulté à synchroniser des tâches

Gestion des ressources partagées

Problème de predictibilité

Problème de reproductibilitéConcrètement:

Thérapie par radiation: Therac-25, entre 85 et 87Blackout au Nord-Est des US en 2003

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 7 / 42

Page 13: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Introduction

Problèmes - Blackout

Blackout au Nord-Est des US en 2003 (55 millions de personnestouchées)

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 8 / 42

Page 14: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Introduction

Problèmes - Radiations

Thérapie par radiation: Therac-25, 6 accidents entre 85 et 87

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 9 / 42

Page 15: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Introduction

Programmation parallèle vs. concurrente

Programmation parallèle (ou répartie)Des processus s’exécutent sur plusieurs processeurs

Programmation concurrenteLes tâches sont gérées par un même processeur

Les mécanismes de synchronisation sont différents

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 10 / 42

Page 16: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Introduction

Question

Soient les deux tâche suivantes (s’exécutant en pseudo-parallèle):

Tâche A

x = 3;

printf("%d",x);

Tâche B

x = 5;

Quelle est la sortie du programme dans le terminal ?

Que vaut x ?

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 11 / 42

Page 17: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Thread

Thread

Un thread est un fil d’exécution

Un processus est composé de plusieurs threadsLes threads s’exécutent en "parallèle"

Sur un mono-processeur: chacun son tourPar entrelacement

Sur un multi-processeur: peut être réellement parallèle

Dans tous les cas, l’ordonnanceur est responsable de l’ordre d’exécutionProblèmes:

Accès à des ressources partagéesEchange de donnéesSéquentialité de l’exécution

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 12 / 42

Page 18: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Thread

Exemple: avance parallèle

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 13 / 42

Page 19: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Thread

Exemple: demande d’accès à la ressource

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 14 / 42

Page 20: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Thread

Exemple: accès à la ressource partagée

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 15 / 42

Page 21: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Thread

Exemple: relâchement de la ressource

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 16 / 42

Page 22: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Thread

Exemple: accès à la ressource partagée

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 17 / 42

Page 23: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Thread

Exemple: avance parallèle

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 18 / 42

Page 24: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Exemple

Problème des threads: exemple

Classe GiveId

class GiveId {private:

int nb_id;public:

GiveId(): nb_id(0) {};int getUniqueId() {

return nb_id ++;}

}

Que signifie return nb_id ++; ?

int temp = nb_id;nb_id = nb_id + 1;return temp;

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 19 / 42

Page 25: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Exemple

Problème des threads: exemple

Classe GiveId

class GiveId {private:

int nb_id;public:

GiveId(): nb_id(0) {};int getUniqueId() {

return nb_id ++;}

}

Que signifie return nb_id ++; ?

int temp = nb_id;nb_id = nb_id + 1;return temp;

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 19 / 42

Page 26: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Exemple

Problème des threads: exemple

Classe GiveId

class GiveId {private:

int nb_id;public:

GiveId(): nb_id(0) {};int getUniqueId() {

return nb_id ++;}

}

Que signifie return nb_id ++; ?

int temp = nb_id;nb_id = nb_id + 1;return temp;

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 19 / 42

Page 27: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Exemple

Problèmes des threads: exemple

int temp = nb_id;nb_id = nb_id + 1;return temp;

Condition initale:nb_id = 0;

Exemple de déroulement

Thread A Thread B

. .

temp = 0 .. temp = 0. nb_id++. return 0

nb_id++ .return 0 .

. .

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 20 / 42

Page 28: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Exemple

Problèmes des threads: exemple

int temp = nb_id;nb_id = nb_id + 1;return temp;

Condition initale:nb_id = 0;

Exemple de déroulement

Thread A Thread B

. .temp = 0 .

. temp = 0

. nb_id++

. return 0nb_id++ .return 0 .

. .

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 20 / 42

Page 29: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Exemple

Problèmes des threads: exemple

int temp = nb_id;nb_id = nb_id + 1;return temp;

Condition initale:nb_id = 0;

Exemple de déroulement

Thread A Thread B

. .temp = 0 .

. temp = 0

. nb_id++

. return 0nb_id++ .return 0 .

. .

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 20 / 42

Page 30: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Exemple

Problèmes des threads: exemple

int temp = nb_id;nb_id = nb_id + 1;return temp;

Condition initale:nb_id = 0;

Exemple de déroulement

Thread A Thread B

. .temp = 0 .

. temp = 0

. nb_id++

. return 0nb_id++ .return 0 .

. .

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 20 / 42

Page 31: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Exemple

Problèmes des threads: exemple

int temp = nb_id;nb_id = nb_id + 1;return temp;

Condition initale:nb_id = 0;

Exemple de déroulement

Thread A Thread B

. .temp = 0 .

. temp = 0

. nb_id++

. return 0

nb_id++ .return 0 .

. .

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 20 / 42

Page 32: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Exemple

Problèmes des threads: exemple

int temp = nb_id;nb_id = nb_id + 1;return temp;

Condition initale:nb_id = 0;

Exemple de déroulement

Thread A Thread B

. .temp = 0 .

. temp = 0

. nb_id++

. return 0nb_id++ .

return 0 .. .

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 20 / 42

Page 33: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Exemple

Problèmes des threads: exemple

int temp = nb_id;nb_id = nb_id + 1;return temp;

Condition initale:nb_id = 0;

Exemple de déroulement

Thread A Thread B

. .temp = 0 .

. temp = 0

. nb_id++

. return 0nb_id++ .return 0 .

. .

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 20 / 42

Page 34: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Exemple

Problèmes des threads: exemple

int temp = nb_id;nb_id = nb_id + 1;return temp;

Condition initale:nb_id = 0;

Exemple de déroulement

Thread A Thread B

. .temp = 0 .

. temp = 0

. nb_id++

. return 0nb_id++ .return 0 .

. .

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 20 / 42

Page 35: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Implémentation

Implémentation

Comment implémenter la concurrence?

Grâce à des mécanismes du langage de programmationGrâce à des bibliothèquesSolution intermédiaire

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 21 / 42

Page 36: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Implémentation

Implémentation

Comment implémenter la concurrence?Grâce à des mécanismes du langage de programmation

Grâce à des bibliothèquesSolution intermédiaire

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 21 / 42

Page 37: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Implémentation

Implémentation

Comment implémenter la concurrence?Grâce à des mécanismes du langage de programmationGrâce à des bibliothèquesSolution intermédiaire

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 21 / 42

Page 38: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Implémentation

Grâce au langage

Avantages:Les notions concurrentes de même que les constructions sont données parle langageDétection d’une partie des erreurs à la compilationMéthodologie de programmation imposée par le langage

Désavantages :Obligation d’utiliser un langage dédié qui est potentiellement peu répanduContraintes liées au langage choisi (pas forcément souhaitable)

Exemples : ADA, Java, C++11, etc.

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 22 / 42

Page 39: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Implémentation

Grâce à des bibliothèques

Le système d’exploitation offre une bibliothèque appelée systèmemulti-tâcheAvantage:

Un langage quelconque peut profiter de la bibliothèqueDésavantages:

La portabilité (dépendance au système cible)Déboguage délicatPas de méthodologie de programmation imposée

Exemple: langage C ou C++ avec bibliothèque POSIX Threads aussiappelée Pthreads

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 23 / 42

Page 40: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Implémentation

Solution intermédiaire

Un précompilateur gère l’implémentation des outilsExemple: C++ avec librairie Qt

Code indépendant de la plateforme cible

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 24 / 42

Page 41: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Implémentation

Exemple de langage: Ada

PourLe langage intègre des notions de concurrenceCertaines vérifications peuvent être faites à la compilationRobuste

ContreMoins utilisé que les autres en pratique

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 25 / 42

Page 42: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Implémentation

Exemple de langage: Java

PourOrienté objetMécanismes de synchronisation natifs

Threadsynchronizedwait, notify, notifyAll

ContreLangage interprétéDonc: légèrement plus lent que C/C++Définition un peu légère du fonctionnement des primitives

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 26 / 42

Page 43: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Implémentation

Exemple de langage: C/C++ (POSIX)

Utilisation de la bibliothèque POSIXPour

Très utilisé dans le monde (notamment embarqué)Code compilé (rapidité)

ContrePas de mécanismes de synchronisation natifs

Stroustrup: "It is possible to design concurrency support libraries thatapproach built-in concurrency support both in convenience and efficiency.By relying on libraries, you can support a variety of concurrency models,..."1

1Parallel and Distributed Programming Using C++, Hughes et HughesY. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 27 / 42

Page 44: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Implémentation

Exemple de langage: C/C++ (POSIX)

Utilisation de la bibliothèque POSIXPour

Très utilisé dans le monde (notamment embarqué)Code compilé (rapidité)

ContrePas de mécanismes de synchronisation natifsStroustrup: "It is possible to design concurrency support libraries thatapproach built-in concurrency support both in convenience and efficiency.By relying on libraries, you can support a variety of concurrency models,..."1

1Parallel and Distributed Programming Using C++, Hughes et HughesY. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 27 / 42

Page 45: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Implémentation

Exemple de langage: C++11

C++11 introduit des classes liées à la concurrence:thread, mutex, condition_variable, atomic, futureEt des classes proches

PourContenu dans le langageLancement des threads facilité

ContrePas de sémaphoresIl faut disposer d’un compilateur supportant C++11

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 28 / 42

Page 46: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Implémentation

Exemple de langage: C/C++ (Qt)

Utilisation de l’environnement QtPour

Interopérabilité (Linux, Linux embarqué, Windows, Mac OS, Android,iOS)Tous les objets nécessaires existent

QThread, QMutex, QSemaphore, QWaitConditionOrienté objet

ContreNécessite d’avoir un portage pour la plateforme

De plus en plus courant

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 29 / 42

Page 47: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Séparation d’un programme en plusieurs threads

Séparation d’un programme en threads

Comment décomposer un programme en plusieurs threads?Il existe plusieurs modèles

Le modèle délégation (boss-worker model ou delegation model en anglais)Le modèle pair (peer model en anglais)Le modèle pipeline (pipeline model en anglais)

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 30 / 42

Page 48: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Séparation d’un programme en plusieurs threads

Modèle délégation

Un thread principal

Des threads travailleurs

tâche1

tâche2

tâche3

entrée

patron

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 31 / 42

Page 49: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Séparation d’un programme en plusieurs threads

Modèle délégation: exemple 1

void *patron(void *) {boucle infinie {

attend une requêteswitch (requete) {

case requeteX: startThread( ... tacheX); break;case requeteY: startThread( ... tacheY); break;...

}}

}

void *tacheX(void *) {exécuter le travail demandé, puis se terminer

}

void *tacheY(void *) {exécuter le travail demandé, puis se terminer

}

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 32 / 42

Page 50: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Séparation d’un programme en plusieurs threads

Modèle délégation: exemple 2

void *patron(void *) {// crée tous les threadsstartThread(...);boucle infinie {

attend une requête;place la requête dans la file d’attentesignale aux travailleurs qu’une requête est prête

}}

void *travailleur(void *) {boucle infinie {

bloque jusqu’à être activé par le patronrécupère la requête de la file d’attenteswitch(requete){

case requeteX: tacheX();case requeteY: tacheY();...

}}

}

void tacheX() {exécuter le travail demandé

}

void tacheY() {exécuter le travail demandé

}

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 33 / 42

Page 51: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Séparation d’un programme en plusieurs threads

Modèle pair

Pas de thread principalTous égauxChacun s’arrange avec ses entrées/sorties

tâche1

tâche2

tâche3

entrées

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 34 / 42

Page 52: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Séparation d’un programme en plusieurs threads

Modèle pair: exemple

main() {startThread( ... tache1);startThread( ... tache2);...signale aux threads qu’ils peuvent commencer à travailler

}

tache1() {attend le signal de commencementeffectue le traitement, et synchronise avec les autres threadssi nécessaire

}

tache2() {attend le signal de commencementeffectue le traitement, et synchronise avec les autres threadssi nécessaire

}

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 35 / 42

Page 53: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Séparation d’un programme en plusieurs threads

Modèle pipeline

Appliqué lorsque:L’application traite une longue chaîne d’entrée;Le traitement à effectuer sur ces entrée peut être décomposé ensous-tâches (étages de pipeline) au travers desquelles chaque donnéed’entrée doit passer;Chaque étage peut traiter une donnée différente à chaque instant.

Un thread attend les données du précédent

Et les transmet ensuite au suivant

tâche1 tâche2 tâche3

entrée sortie

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 36 / 42

Page 54: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Séparation d’un programme en plusieurs threads

Modèle pipeline: exemple (1)

etage1() {boucle infinie {

récupérer une entrée du programmetraiter cette donnéepasser le résultat à l’étage suivant

}}etage2() {

boucle infinie {récupérer une donnée de l’étage précédenttraiter cette donnéepasser le résultat à l’étage suivant

}}etageN() {

boucle infinie {récupérer une donnée de l’étage précédenttraiter cette donnéepasser le résultat en sortie du programme

}}Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 37 / 42

Page 55: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Séparation d’un programme en plusieurs threads

Modèle pipeline: exemple (2)

main() {startThread( ... etage1);startThread( ... etage2);...startThread( ... etageN);...

}

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 38 / 42

Page 56: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Séparation d’un programme en plusieurs threads

Exemple d’EzeCHieL

Logiciel d’aide à l’interprétation de mesures de concentration pourajustement de posologies (pour pharmacologie clinique)

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 39 / 42

Page 57: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Séparation d’un programme en plusieurs threads

Exemples sur le projet EzeCHieL

Les médicaments sont décrits par des fichiers XMLIls doivent être chargés au lancement du programme⇒ Chargement géré par un thread

Evite de ralentir le lancement du programme

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 40 / 42

Page 58: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Séparation d’un programme en plusieurs threads

Exemples sur le projet EzeCHieL

Certains traitements mathématiques nécessaires pour afficher une courbesont longs (quelques secondes)⇒ Traitement géré par un thread

Evite de freezer l’application pendant le traitement

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 41 / 42

Page 59: Introduction à la programmation concurrente - Introduction...Plan 1 Introduction 2 Thread 3 Exemple 4 Implémentation 5 Séparation d’un programme en plusieurs threads Y. Thoma

Séparation d’un programme en plusieurs threads

Exemples sur le projet EzeCHieL

Calcul de courbes de percentiles selon Monte CarloIl faut calculer 3000 courbes⇒ Multithreadé

Il faut trier ces courbes, à chaque temps⇒ Multithreadé

Y. Thoma (HES-SO / HEIG-VD / REDS) Introduction à la programmation concurrente Février 2017 42 / 42


Recommended