50 lines
No EOL
947 B
C
50 lines
No EOL
947 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <pthread.h>
|
|
#include <unistd.h>
|
|
|
|
// Ressources disponible
|
|
int N = 3;
|
|
|
|
// Once
|
|
pthread_once_t once = PTHREAD_ONCE_INIT;
|
|
pthread_key_t key;
|
|
|
|
int incrementation() {
|
|
int *s_int;
|
|
s_int = (int*)pthread_getspecific(key);
|
|
return ++(*s_int);
|
|
}
|
|
|
|
void fonction_init() {
|
|
printf("Initialisation...\n");
|
|
pthread_key_create(&key, NULL);
|
|
}
|
|
|
|
void * fn_thread (void * n) {
|
|
pthread_once(&once, fonction_init);
|
|
|
|
int* s_int;
|
|
s_int = malloc(sizeof(int));
|
|
*s_int = pthread_self();
|
|
pthread_setspecific(key, (void *)s_int);
|
|
|
|
for (int i = 0; i < N; ++i){
|
|
printf("Boucle : %d\n", incrementation());
|
|
}
|
|
pthread_exit(0);
|
|
}
|
|
|
|
int main () {
|
|
pthread_t p1;
|
|
pthread_t p2;
|
|
|
|
pthread_create (& p1, NULL, fn_thread, NULL);
|
|
pthread_create (& p2, NULL, fn_thread, NULL);
|
|
fn_thread(NULL);
|
|
|
|
pthread_join(p1, NULL);
|
|
pthread_join(p2, NULL);
|
|
|
|
return 0;
|
|
} |