Pointeur sur structure pour faire tourner plusieurs threads en même temps
SMDH
-
Dalfab Messages postés 720 Statut Membre -
Dalfab Messages postés 720 Statut Membre -
Bonjour, la question est simple, je n'arrive pas à faire passer mon pointeur sur ma structure afin d'afficher mes threads qui tournent en même temps. Quelqu'un pourrait m'aider et surtout m'expliquer ? Merci !
Voici mon code :
Aucune erreur quand je compile avec :
Mais quand j'exécute avec
J'obtiens :
Voici mon code :
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
struct plusieurs_param
{
int nbr_chants;
char chant[30];
};
void *supporter (void* p){
struct plusieurs_param *pp = (struct plusieurs_param*) p;
printf ("%d", pp->nbr_chants);
char str[30];
strcpy(str,(char *) pp->chant);
printf("%d\n", pp->nbr_chants),
printf("%s\n", pp->chant);
int i;
//int pid;
pthread_t tid;
//pid = getpid();
tid = pthread_self();
srand ((int) tid);
for (i = 0; i < pp->nbr_chants; i++){
//printf ("Processus %d Thread %x : %s \n", pid, (unsigned int) tid, str);
usleep (rand() / RAND_MAX * 1000000);
}
return (void*) tid;
}
int main (int argc, char **argv){
int team1;
int team2;
int i;
int nb_threads = 0;
pthread_t *tids;
if (argc != 5){
fprintf(stderr, "usage : %s team1 nbrchant1 team2 nbrchant2\n", argv[0]);
exit (-1);
}
team1 = atoi (argv[1]);
team2 = atoi (argv[3]);
nb_threads = team1 + team2;
tids = malloc(nb_threads*sizeof(pthread_t));
/*Create the threads for team1*/
struct plusieurs_param *p1;
p1 = malloc(sizeof(struct plusieurs_param));
p1->nbr_chants = atoi (argv[2]);
char tab1[29] = "Allons enfants de la patrie";
strcpy(p1->chant, tab1);
printf("%d\n", p1->nbr_chants);
printf("%s\n", p1->chant);
for (i = 0; i < team1; i++){
pthread_create (&tids[i], NULL, supporter, &p1);
}
/*Create the other threads (ie. team2)*/
struct plusieurs_param *p2;
p2 = malloc(sizeof(struct plusieurs_param));
p2->nbr_chants =atoi (argv[4]);
char tab2[26] = "Swing low, sweet chariot";
strcpy(p2->chant, tab2);
printf("%d\n", p2->nbr_chants);
printf("%s\n", p2->chant);
for ( ; i < nb_threads; i++){
pthread_create (&tids[i], NULL, supporter, &p2);
}
/*Wait until every thread end*/
for (i = 0; i < nb_threads; i++){
pthread_join (tids[i], NULL);
}
free(tids);
return EXIT_SUCCESS;
}
Aucune erreur quand je compile avec :
gcc -Wall -o matchp matchp.c -lpthread
Mais quand j'exécute avec
./matchp 4 2 2 5
J'obtiens :
2
Allons enfants de la patrie
997446304997446304
�U
997446304997446304
�U
997446304997446304997446304
�U
997446304
5
Swing low, sweet chariot
�U
997448544997448544
�U
997448544997448544
�U
A voir également:
- Pointeur sur structure pour faire tourner plusieurs threads en même temps
- Renommer plusieurs fichiers en même temps - Guide
- Excel trier plusieurs colonnes en même temps - Guide
- Tourner ecran windows - Guide
- Pointeur souris - Guide
- Curseur Windows 10 : comment bien voir la souris à l'écran - Guide