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
| #include <stdio.h>
#include <stdlib.h>
#include "pthread.h"
void *f (void *arg);
void *test (void *arg);
int cpt=0;
pthread_mutex_t mutex;
pthread_t thread1, thread2;
int main ()
{ pthread_mutex_init(&mutex, NULL);
pthread_create(&thread1,NULL,f,(void *)"t1");
pthread_create(&thread2,NULL,test,(void *)"t2");
pthread_mutex_destroy(&mutex);
return 0; }
void *f (void *arg)
{ bool encore=true;
while (encore)
{ pthread_mutex_lock(&mutex);
cpt++;
if (cpt == 10)
encore=false;
pthread_mutex_unlock(&mutex);
}
pthread_exit(0);
}
void *stop (void *arg)
{
bool cancel=false;
while (!cancel)
{ pthread_mutex_lock(&mutex);
if (cpt < 5)
{ cancel=true;
printf("On arrete le thread 1\n");
pthread_cancel(thread1);
}
pthread_mutex_unlock(&mutex);
}
pthread_exit(0);
} |