[C LINUX]Création de threads

Fermé
renaudh Messages postés 138 Date d'inscription lundi 17 novembre 2003 Statut Membre Dernière intervention 2 septembre 2016 - 16 nov. 2005 à 15:29
Char Snipeur Messages postés 9696 Date d'inscription vendredi 23 avril 2004 Statut Contributeur Dernière intervention 3 octobre 2023 - 16 nov. 2005 à 22:51
Bonjour, je dois réaliser le programme qui lance l'exécution en parallèle des 3 bouts de code suivants:

T1: P(Y);
       R := R*2;
       V(Y);

T2: P(Y);
       R := R*R;
       V(Y);

T3: P(Y);
       R := R+3;
       V(Y);


Voici le programme que j'ai tapé pour réaliser ceci:

#include <stdio.h>
#include <pthread.h>

int r=0;

#define NB_THREADS 3

pthread_t callThd[NB_THREADS];
pthread_mutex_t y;

void *t1 (void *arg)
{
		pthread_mutex_lock (&y);
		r=r*2;
		pthread_mutex_unlock (&y);
		pthread_exit(NULL);
}

void *t2 (void *arg)
{
		pthread_mutex_lock (&y);
		r=r*r;
		pthread_mutex_unlock (&y);
		pthread_exit(NULL);
}

void *t3 (void *arg)
{
		pthread_mutex_lock (&y);
		r=r+3;
		pthread_mutex_unlock (&y);
		pthread_exit(NULL);
}

int main (void)
{
		int status,t;
		
		pthread_mutex_init(&y, 1);
		
		pthread_attr_setdetachstate(NULL, PTHREAD_CREATE_JOINABLE);
		
		for (t=0; t<NB_THREADS;t++)
		{
				switch (t)
				{
						case 0 :
						printf("%d\n", r);
						pthread_create(&callThd[t], NULL, t1, (void *)t);
						printf("%d\n", r);
						break;
						
						case 1 :
						printf("%d\n", r);
						pthread_create(&callThd[t], NULL, t2, (void *)t);
						printf("%d\n", r);
						break;
						
						case 2 :
						printf("%d\n", r);
						pthread_create(&callThd[t], NULL, t3, (void *)t);
						printf("%d\n", r);
						break;
				}			
		}
		
		for (t=0; t<NB_THREADS; t++)
		{
				pthread_join (callThd[t], (void **)&status);
		}
		
		pthread_mutex_destroy(&y);
		pthread_exit(NULL);
}


Il m'indique un Warning à la ligne 39: passing arg 2 of 'pthread_mutex_init' makes pointer from integer without a cast.

Soit seulement un warning donc j'essaie de lancer le programme et ça m'indique Bus Error (core dumped)

Si quelqu'un pouvait y regarder attentivement, ça m'aiderait bcp, vous pouvez aussi me contacter via msn sur hermanrenaud@gmail.com

Merci pour votre aide.
A voir également:

2 réponses

renaudh Messages postés 138 Date d'inscription lundi 17 novembre 2003 Statut Membre Dernière intervention 2 septembre 2016 3
16 nov. 2005 à 15:47
G remplacé le 1 par NULL et maintenant ça fonctionne mais il reste un petit problème, il ne passe jamais dans la thread 3 et je me demande bien pourquoi!
0
Char Snipeur Messages postés 9696 Date d'inscription vendredi 23 avril 2004 Statut Contributeur Dernière intervention 3 octobre 2023 1 297
16 nov. 2005 à 22:51
En effet, c'est bizard.
mais comme tu passe t dans tes thread, ça peut se comprendre.
est tu sur que t n'est pas modifer dans tes apel?
Je ne connait pas très bien les thread, mais passer t en dernier arguments me parait etrange.
Est -ce que ton Linux te fait bien les thread en parallèle.
Avec celui que j'utilise, il n'utilise qu'un seul processeur malgrés les multiple thread.
0