Trouver une ressource (Nouvelle version du moteur, plus rapide & pertinent, essayez le !)
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
THREADS SOUS LINUX
Information sur la source
Description
Ce code montre très simplement comment utiliser des threads sous linux. Pour compiler la source, il faut linker avec la librairie "pthread" soit : g++ thread.cpp -o thread -lpthread Vous pouvez utiliser gcc mais dans ce cas, il faut remplacer le "new char" par un "malloc"... Mise à jour le 13/01/04 : Rajout de l'utilisation de mutex et de pthread_join. Avis aux amateurs ;) Rajout de la source en zip.
Source
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <unistd.h> // pour sleep
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
-
- // mutex => MUtuelle EXclusion (permet d'éviter un accès simultané à une variable)
- static pthread_mutex_t mutex;
- static int tab[5];
-
-
- void* threadLectureTableau(void* arg)
- {
- for (int i = 0 ; i != 5 ; i++)
- {
- // blocage du mutex
- // (attente éventuelle de sa libération s'il est déjà bloqué)
- pthread_mutex_lock (&mutex);
-
- printf ("Lecture du tableau 1 : tab[%d] vaut %d\\n", i, tab[i]);
-
- // déblocage du mutex
- pthread_mutex_unlock (&mutex);
-
- sleep(1);
- }
-
- pthread_exit (0);
- }
-
-
- void* threadLectureTableau2(void* arg)
- {
- for (int i = 0 ; i != 5 ; i++)
- {
- // blocage du mutex
- // (attente éventuelle de sa libération s'il est déjà bloqué)
- pthread_mutex_lock (&mutex);
-
- printf ("Lecture du tableau 2 : tab[%d] vaut %d\\n", i, tab[i]);
-
- // déblocage du mutex
- pthread_mutex_unlock (&mutex);
-
- sleep(2);
- }
-
- pthread_exit (0);
- }
-
-
- void* threadEcritureTableau(void* arg)
- {
- // blocage du mutex
- // (attente éventuelle de sa libération s'il est déjà bloqué)
- pthread_mutex_lock (&mutex);
-
- for (int i = 0 ; i != 5 ; i++)
- {
- tab[i] = 2 * i;
- printf ("Ecriture du tableau : tab[%d] vaut %d\\n", i, tab[i]);
- sleep(1);
- }
-
- // déblocage du mutex
- pthread_mutex_unlock (&mutex);
-
- pthread_exit (0);
- }
-
-
- int main(int argc, char *argv[])
- {
- pthread_t thread1, thread2, thread3;
- void *retour;
-
- pthread_mutex_init (&mutex, NULL);
-
- if (pthread_create (&thread1, NULL, threadEcritureTableau, NULL) < 0)
- {
- fprintf (stderr, "Impossible de créer le thread 1\\n");
- exit (1);
- }
-
- if (pthread_create (&thread2, NULL, threadLectureTableau, NULL) < 0)
- {
- fprintf (stderr, "Impossible de créer le thread 2\\n");
- exit (1);
- }
-
- if (pthread_create (&thread3, NULL, threadLectureTableau2, NULL) < 0)
- {
- fprintf (stderr, "Impossible de créer le thread 2\\n");
- exit (1);
- }
-
- // le processus principale est ainsi contraint d'attendre la fin d'exécution
- // des threads fils
- (void)pthread_join (thread1, &retour);
- (void)pthread_join (thread2, &retour);
- (void)pthread_join (thread3, &retour);
- }
-
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h> // pour sleep
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
// mutex => MUtuelle EXclusion (permet d'éviter un accès simultané à une variable)
static pthread_mutex_t mutex;
static int tab[5];
void* threadLectureTableau(void* arg)
{
for (int i = 0 ; i != 5 ; i++)
{
// blocage du mutex
// (attente éventuelle de sa libération s'il est déjà bloqué)
pthread_mutex_lock (&mutex);
printf ("Lecture du tableau 1 : tab[%d] vaut %d\\n", i, tab[i]);
// déblocage du mutex
pthread_mutex_unlock (&mutex);
sleep(1);
}
pthread_exit (0);
}
void* threadLectureTableau2(void* arg)
{
for (int i = 0 ; i != 5 ; i++)
{
// blocage du mutex
// (attente éventuelle de sa libération s'il est déjà bloqué)
pthread_mutex_lock (&mutex);
printf ("Lecture du tableau 2 : tab[%d] vaut %d\\n", i, tab[i]);
// déblocage du mutex
pthread_mutex_unlock (&mutex);
sleep(2);
}
pthread_exit (0);
}
void* threadEcritureTableau(void* arg)
{
// blocage du mutex
// (attente éventuelle de sa libération s'il est déjà bloqué)
pthread_mutex_lock (&mutex);
for (int i = 0 ; i != 5 ; i++)
{
tab[i] = 2 * i;
printf ("Ecriture du tableau : tab[%d] vaut %d\\n", i, tab[i]);
sleep(1);
}
// déblocage du mutex
pthread_mutex_unlock (&mutex);
pthread_exit (0);
}
int main(int argc, char *argv[])
{
pthread_t thread1, thread2, thread3;
void *retour;
pthread_mutex_init (&mutex, NULL);
if (pthread_create (&thread1, NULL, threadEcritureTableau, NULL) < 0)
{
fprintf (stderr, "Impossible de créer le thread 1\\n");
exit (1);
}
if (pthread_create (&thread2, NULL, threadLectureTableau, NULL) < 0)
{
fprintf (stderr, "Impossible de créer le thread 2\\n");
exit (1);
}
if (pthread_create (&thread3, NULL, threadLectureTableau2, NULL) < 0)
{
fprintf (stderr, "Impossible de créer le thread 2\\n");
exit (1);
}
// le processus principale est ainsi contraint d'attendre la fin d'exécution
// des threads fils
(void)pthread_join (thread1, &retour);
(void)pthread_join (thread2, &retour);
(void)pthread_join (thread3, &retour);
}
Conclusion
Voilà, j'ai étoffé un peu le code, à la demande de certains, montrant ainsi une nouvelle notion très importante dans la gestion en temps réel, les mutex.
Fichier Zip
Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !
Télécharger le zip
Sources de la même categorie
Commentaires
|
CalendriCode
| | | L | M | M | J | V | S | D |
| 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 | | | | | |
|
|