IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

POSIX C Discussion :

Le rôle de la librairie pthreads ?


Sujet :

POSIX C

  1. #1
    Membre actif
    Homme Profil pro
    Inscrit en
    Octobre 2007
    Messages
    487
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2007
    Messages : 487
    Points : 294
    Points
    294
    Par défaut Le rôle de la librairie pthreads ?
    Voila je suis entrain d’apprendre la programmation C sous linux
    Le prof nous a donné ce code
    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
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <pthred.h>
    void *fonction_de_thread(void * arg);
    char message[]="hello word";
    int main(){
    	int res;
    	pthread_t un _thread;
    	void *resultat_de_thread;
    	res=pthred_create(&un_thread,NULL,fonction_de_thread,(void*)message);//fork
    	if(res!=0){
    		perror("echec de la creation du thread");
    		exit(EXIT_FAILURE);
    	}
    	printf("eb attente de terminaison du thread");//wait
    	if(res!=0){
    		perror("echec de l'ajout du thread");
    		exit(EXIT_FAILURE);
    	}
    	printf("retour du thread, il a renvoyé %s\n"(char *) resultat_de_thread);
    	printf("voici a present le message %s\n",message);
    	exit(EXIT_SUCCESS);
    }
    void * fonction_de_thread(void * arg){
    	printf("la fonction_de_thrad est en cours d'execution,l'argument etait %s\n",(char *)arg);
    	sleep(3);
    	strcpy(message,"salut!");
    	pthread_exit("Merci pour le temps processeur");
    }
    A quoi sert pthread ?
    Et qu est ce qu’il fait ce code ?

  2. #2
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 518
    Points
    41 518
    Par défaut
    La bibliothèque pthread (POSIX Threads) permet de faire de la programmation multithread sous POSIX.
    En gros, cela permet d'avoir deux fonctions qui s'exécutent simultanément, chacune sur son thread.

    Par contre, il y a des erreurs dans ce code: La déclaration de un_thread a un espace de trop, et il manque l'appel à la fonction pthread_join() entre le printf(...)//wait et le if(res!=0)...
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  3. #3
    Membre actif
    Homme Profil pro
    Inscrit en
    Octobre 2007
    Messages
    487
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2007
    Messages : 487
    Points : 294
    Points
    294
    Par défaut
    Je n’ai pas comprit ses deux lignes
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    void *resultat_de_thread;
    	res=pthred_create(&un_thread,NULL,fonction_de_thread,(void*)message);//fork
    Et pour quoi void * fonction_de_thread(void * arg) vient après la main?

  4. #4
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 518
    Points
    41 518
    Par défaut
    1. La première déclare un pointeur qui sera modifié lors de l'appel à pthread_join() (qui manque). La seconde lance le nouveau thread, indiquant que son point d'entrée est la fonction fonction_de_thread().
    2. Pour rien. On aurait aussi bien pu la définir avant le main(). L'important, c'est qu'elle soit au moins déclarée avant le main(), et c'est le cas.
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  5. #5
    Membre actif
    Homme Profil pro
    Inscrit en
    Octobre 2007
    Messages
    487
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2007
    Messages : 487
    Points : 294
    Points
    294
    Par défaut
    Qu’elles sont les parametres de pthread_creat et
    pthread_join(un _thread,NULL)
    res=pthred_create(&un_thread,NULL,fonction_de_thread,(void*)message)
    et

  6. #6
    Rédacteur
    Avatar de Vincent Rogier
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    2 373
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 2 373
    Points : 5 307
    Points
    5 307
    Par défaut
    Bonsoir,

    Je penses que tu devrais lire ceci :

    Tutoriel : Initiation à la programmation multitâche en C avec Pthreads, par Franck Hecht
    Vincent Rogier.

    Rubrique ORACLE : Accueil - Forum - Tutoriels - FAQ - Livres - Blog

    Vous voulez contribuer à la rubrique Oracle ? Contactez la rubrique !

    OCILIB (C Driver for Oracle)

    Librairie C Open Source multi-plateformes pour accéder et manipuler des bases de données Oracle

  7. #7
    Membre actif
    Homme Profil pro
    Inscrit en
    Octobre 2007
    Messages
    487
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2007
    Messages : 487
    Points : 294
    Points
    294
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    void *resultat_de_thread;
    pour quoi on l'a declarer comme un pointeur?

  8. #8
    Expert confirmé
    Avatar de Thierry Chappuis
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Mai 2005
    Messages
    3 499
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Suisse

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 499
    Points : 5 508
    Points
    5 508
    Par défaut
    Citation Envoyé par Médinoc Voir le message
    La bibliothèque pthread (POSIX Threads) permet de faire de la programmation multithread sous POSIX.
    Pas seulement sous POSIX d'ailleurs. pthreads est aussi utilisable sous Windows.

    Thierry
    "The most important thing in the kitchen is the waste paper basket and it needs to be centrally located.", Donald Knuth
    "If the only tool you have is a hammer, every problem looks like a nail.", probably Abraham Maslow

    FAQ-Python FAQ-C FAQ-C++

    +

  9. #9
    Rédacteur
    Avatar de Vincent Rogier
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    2 373
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 2 373
    Points : 5 307
    Points
    5 307
    Par défaut
    Citation Envoyé par Thierry Chappuis Voir le message
    Pas seulement sous POSIX d'ailleurs. pthreads est aussi utilisable sous Windows.

    Thierry
    POSIX n'est (dans l'absolu) pas synonyme d'Unix (bien que dans la pratique ...).

    Tout système peut être conforme POSIX

    Windows NT (3.5 et > ) est conforme POSIX 1. (Plus de détails ici)
    Vincent Rogier.

    Rubrique ORACLE : Accueil - Forum - Tutoriels - FAQ - Livres - Blog

    Vous voulez contribuer à la rubrique Oracle ? Contactez la rubrique !

    OCILIB (C Driver for Oracle)

    Librairie C Open Source multi-plateformes pour accéder et manipuler des bases de données Oracle

  10. #10
    Expert éminent sénior
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Points : 20 985
    Points
    20 985
    Par défaut
    Citation Envoyé par dot-_-net Voir le message
    Voila je suis entrain d’apprendre la programmation C sous linux
    Le prof nous a donné ce code
    <...>
    A quoi sert pthread ?
    Et qu est ce qu’il fait ce code ?
    Il vous balance ça comme ça sans explications ou tu étais à la manif ?

    http://emmanuel-delahaye.developpez.com/pthreads.htm
    Pas de Wi-Fi à la maison : CPL

  11. #11
    Membre actif
    Homme Profil pro
    Inscrit en
    Octobre 2007
    Messages
    487
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2007
    Messages : 487
    Points : 294
    Points
    294
    Par défaut
    Citation Envoyé par Emmanuel Delahaye Voir le message
    Il vous balance ça comme ça sans explications ou tu étais à la manif ?

    http://emmanuel-delahaye.developpez.com/pthreads.htm
    lol
    Merci Emmanuel Delahaye j’ai comprit pthread

  12. #12
    Expert confirmé
    Avatar de Thierry Chappuis
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Mai 2005
    Messages
    3 499
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Suisse

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 499
    Points : 5 508
    Points
    5 508
    Par défaut
    Citation Envoyé par vicenzo Voir le message
    POSIX n'est (dans l'absolu) pas synonyme d'Unix (bien que dans la pratique ...).

    Tout système peut être conforme POSIX

    Windows NT (3.5 et > ) est conforme POSIX 1. (Plus de détails ici)
    Oui, enfin conforme ou pas, pthreads est supporté par Windows via une bibliothèque tièrce.

    Thierry
    "The most important thing in the kitchen is the waste paper basket and it needs to be centrally located.", Donald Knuth
    "If the only tool you have is a hammer, every problem looks like a nail.", probably Abraham Maslow

    FAQ-Python FAQ-C FAQ-C++

    +

  13. #13
    Membre actif
    Homme Profil pro
    Inscrit en
    Octobre 2007
    Messages
    487
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2007
    Messages : 487
    Points : 294
    Points
    294
    Par défaut
    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <pthread.h>
     
    void *fonction(void * arg);
    typedef char chaine[256];
    chaine  *t;
    int nombre_de_ligne;
     
     
    int main(int argc, char* argv[])
    {	
    	int i;
    	pthread_t tr;
    	int res;
    	void *resulta;
    	res = pthread_create(&tr,NULL,fonction,t);
    	if (res!= 0) {
    		printf("Erreur");
    	exit(EXIT_FAILURE);
    	}
    	printf("le thread est entrain de lire le fichier");
    	res=pthread_join(tr,& nombre_de_ligne);
    	if(res!=0){
    		perror("Echec");
    		exit(EXIT_FAILURE);
    	}
    	for(i=0;i<nombre_de_ligne;i++)printf("%s",t[i]);
    	return 0;
    }
     
     
    void * fonction(void * arg){
    	int cp=0;
    	FILE * f= fopen("fichier","r");
    	t=(chaine *) malloc (256 * 1);
    	while(fscanf(f,"%s",&t[cp])>0){
    		cp++;
    		t=(chaine *) realloc(t,256 * (cp+1));
    	}
    	fclose(f);
    	pthread_exit(cp);
    }
    Pour quoi ce code ne marche pas ?

  14. #14
    Membre émérite Avatar de nicolas.sitbon
    Profil pro
    Inscrit en
    Août 2007
    Messages
    2 015
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 2 015
    Points : 2 280
    Points
    2 280
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    main.c:18: erreur: implicit declaration of function «pthread_creat"
    main.c:18: attention : nested extern declaration of «pthread_creat"
    main.c:24: attention : passing argument 2 of «pthread_join" from incompatible pointer type
    main.c:17: attention : unused variable «resulta"
    main.c: In function «fonction":
    main.c:38: attention : format «%s" expects type «char *", but argument 3 has type «char (*)[256]"
    main.c:43: attention : passing argument 1 of «pthread_exit" makes pointer from integer without a cast
    "The quieter you become, the more you are able to hear"
    "Plus vous êtes silencieux, plus vous êtes capable d'entendre"

  15. #15
    Membre actif
    Homme Profil pro
    Inscrit en
    Octobre 2007
    Messages
    487
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2007
    Messages : 487
    Points : 294
    Points
    294
    Par défaut
    Si je veux rien retourné dans mon thread
    puis je mettre pthread_exit(NULL) ;
    Ou bien ne pas l’appeler?

  16. #16
    Expert éminent sénior
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Points : 20 985
    Points
    20 985
    Par défaut
    Citation Envoyé par dot-_-net Voir le message
    Si je veux rien retourné dans mon thread
    puis je mettre pthread_exit(NULL) ;
    Ou bien ne pas l’appeler?
    Ca veut dire quoi 'retourner dans un thread' ? Une fois qu'il est terminé, tout ce que tu peux faire, c'est le relancer avec pthread_create().
    Pas de Wi-Fi à la maison : CPL

  17. #17
    Membre émérite Avatar de nicolas.sitbon
    Profil pro
    Inscrit en
    Août 2007
    Messages
    2 015
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 2 015
    Points : 2 280
    Points
    2 280
    Par défaut
    à priori, d'après la norme, rien n'empêche de passer NULL à pthread_exit().
    "The quieter you become, the more you are able to hear"
    "Plus vous êtes silencieux, plus vous êtes capable d'entendre"

  18. #18
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 518
    Points
    41 518
    Par défaut
    Citation Envoyé par Emmanuel Delahaye Voir le message
    Ca veut dire quoi 'retourner dans un thread' ? Une fois qu'il est terminé, tout ce que tu peux faire, c'est le relancer avec pthread_create().
    Tu te trompes: pthread_join() retourne le code de retour du thread: Le void* retourné par la fonction ou passé à pthread_exit().

    WaitForSingleObject() + GetExitCodeThread() font la même chose sous l'API Windows, mais ne permet pas de retourner un pointeur (ça foirerait sous Win64).

    Edit: Ah, je crois qu'on ne parle pas du même sens du verbe "retourner".
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  19. #19
    Expert éminent sénior
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Points : 20 985
    Points
    20 985
    Par défaut
    Citation Envoyé par dot-_-net Voir le message
    Pour quoi ce code ne marche pas ?
    Euh, avant de se lancer dans les threads, il faudrait déjà appendre le C de base et l'usage de realloc()...

    http://emmanuel-delahaye.developpez....tes.htm#malloc
    http://emmanuel-delahaye.developpez....es.htm#realloc

    Ceci fonctionne :
    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
     
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <pthread.h>
     
    typedef char chaine[256];
     
    void *fonction (void *arg)
    {
       int cp = 0;
       chaine **tt = arg;
       chaine *t = *tt;
       FILE *f = fopen ("data.txt", "r");
       if (f != NULL)
       {
          t = malloc (sizeof *t * (cp + 1));
          if (t != NULL)
          {
             while (fgets (t[cp], sizeof *t, f) != NULL)
             {
                void *tmp;
     
                cp++;
                tmp = realloc (t, sizeof *t * (cp + 1));
     
                if (tmp != NULL)
                {
                   t = tmp;
                }
                else
                {
                   puts ("memory error");
                }
             }
          }
          else
          {
             puts ("memory error");
          }
     
          fclose (f);
       }
       *tt = t;
       pthread_exit ((void *) cp);
       return NULL;
    }
     
    int main (void)
    {
       int i;
       pthread_t tr;
       int res;
       int nombre_de_ligne;
       chaine *t;
     
       printf ("le thread est entrain de lire le fichier... ");
       res = pthread_create (&tr, NULL, fonction, &t);
       if (res != 0)
       {
          printf ("Erreur");
          exit (EXIT_FAILURE);
       }
       res = pthread_join (tr, &nombre_de_ligne);
       if (res != 0)
       {
          perror ("Echec");
          exit (EXIT_FAILURE);
       }
       puts ("lecture terminee");
     
       for (i = 0; i < nombre_de_ligne; i++)
       {
          printf ("%s", t[i]);
     
       }
     
       free (t), t = NULL;
     
       return 0;
    }
    Mais j'ai un warning :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    Project   : Forums
    Compiler  : GNU GCC Compiler (called directly)
    Directory : D:\dev\forums\
    --------------------------------------------------------------------------------
    Switching to target: default
    Compiling: main.c
    main.c: In function `main':
    main.c:63: warning: passing arg 2 of `pthread_join' from incompatible pointer type
    Linking console executable: console.exe
    Process terminated with status 0 (0 minutes, 1 seconds)
    0 errors, 1 warnings
    Pour que ça fonctionne, il faudrait faire ceci, ce qui me parait être d'un absurdité totale...

    Je préconise d'utiliser une structure qui va considérablement simplifier le codage :
    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
     
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <pthread.h>
     
    typedef char chaine[256];
     
    struct data
    {
       chaine *t;
       int cp;
    };
     
    void *fonction (void *arg)
    {
       struct data *p_data = arg;
       if (p_data != NULL)
       {
          FILE *f = fopen ("data.txt", "r");
          if (f != NULL)
          {
             p_data->cp = 0;
             p_data->t = malloc (sizeof *p_data->t * (p_data->cp + 1));
             if (p_data->t != NULL)
             {
                int err = 0;
                while (!err
                       && fgets (p_data->t[p_data->cp], sizeof *p_data->t,
                                 f) != NULL)
                {
                   void *tmp;
     
                   p_data->cp++;
                   tmp =
                      realloc (p_data->t, sizeof *p_data->t * (p_data->cp + 1));
     
                   if (tmp != NULL)
                   {
                      p_data->t = tmp;
                   }
                   else
                   {
                      free (p_data->t), p_data->t = NULL;
                      puts ("memory error");
                      err = 1;
                   }
                }
             }
             else
             {
                puts ("memory error");
             }
     
             fclose (f);
          }
       }
       return NULL;
    }
     
    int main (void)
    {
       pthread_t tr;
       struct data data;
       int res;
     
       printf ("le thread est entrain de lire le fichier... ");
       res = pthread_create (&tr, NULL, fonction, &data);
       if (res != 0)
       {
          printf ("Erreur");
          exit (EXIT_FAILURE);
       }
       res = pthread_join (tr, NULL);
     
       if (res != 0)
       {
          perror ("Echec");
          exit (EXIT_FAILURE);
       }
       puts ("lecture terminee");
       {
          int i;
          for (i = 0; i < data.cp; i++)
          {
             printf ("%s", data.t[i]);
          }
     
          free (data.t), data.t = NULL;
       }
       return 0;
    }
    Pas de Wi-Fi à la maison : CPL

  20. #20
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 518
    Points
    41 518
    Par défaut
    Il y a une histoire de cast de int en pointeur ici...
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. Problème librairie pthread
    Par Hellgast dans le forum Threads & Processus
    Réponses: 1
    Dernier message: 09/07/2010, 11h41
  2. Librairie pthread manquante pour OpenMP sous Windows
    Par jeryagor dans le forum Threads & Processus
    Réponses: 0
    Dernier message: 08/07/2010, 17h12
  3. Linker librairie pthread
    Par storm_2000 dans le forum Eclipse C & C++
    Réponses: 6
    Dernier message: 11/10/2009, 22h54
  4. Probleme de linkage avec la librairie pthread
    Par darkantoine dans le forum Visual C++
    Réponses: 2
    Dernier message: 05/09/2009, 12h32
  5. Réponses: 3
    Dernier message: 17/04/2007, 11h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo