41 lines
No EOL
810 B
C
41 lines
No EOL
810 B
C
#include <stdio.h>
|
|
#include <pthread.h>
|
|
#include <unistd.h>
|
|
|
|
double * px = NULL;
|
|
|
|
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
void * fn_thread (void * p_int) {
|
|
for (int i = 0; i < *(int *) p_int; ++i) {
|
|
pthread_mutex_lock(& mutex);
|
|
printf("%f\n", *px);
|
|
pthread_mutex_unlock(& mutex);
|
|
}
|
|
pthread_exit(0);
|
|
}
|
|
|
|
int main () {
|
|
int max = 0;
|
|
pthread_t p1;
|
|
|
|
printf("Saisir max : \n");
|
|
scanf ("%d", &max);
|
|
printf("Ok ! \n");
|
|
|
|
double x = 1.0;
|
|
px = &x;
|
|
pthread_create (& p1, NULL, fn_thread, (void *) &max);
|
|
|
|
for (int i = 0; i < max; ++i) {
|
|
pthread_mutex_lock(& mutex);
|
|
px = NULL;
|
|
printf("i : %d\n", i);
|
|
px = &x;
|
|
pthread_mutex_unlock(& mutex);
|
|
}
|
|
|
|
pthread_join(p1, NULL);
|
|
|
|
return 0;
|
|
} |