Bonjour,

J'aurais besoin d'un petit coup de main sur la liste chainée suivante, en fait je débute avec les pthreads, et je galère en particulier avec les fonctions pthread_cond_wait et pthread_cond_signal, je ne comprends pas bien ou les placer dans la fonction push pour réduire la charge cpu. A votre bon coeur.

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
 
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
 
struct list {
    int i;
    struct list *next;
};
 
struct list *init(void) {
    struct list *p = malloc(sizeof(struct list));
    p->i = 0;
    p->next = NULL;
    return p;
}
 
static struct list *stack = NULL;
pthread_mutex_t zone = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condition = PTHREAD_COND_INITIALIZER;
 
static void print(struct list *p) {
    printf("%d %p\n", p->i, p->next);
}
 
void *push(struct list *p) {
    int i = 0;
    pthread_mutex_init(&zone, NULL);
    pthread_mutex_lock(&zone);
	pthread_cond_init(&condition, NULL);
    while(i++ < 1000000) {
        stack = p;
        stack->i += 1;
        stack->next = init();
        print(stack);
/*   	pthread_cond_signal(&condition);  */      
/*  	pthread_cond_wait(&condition, &zone);  */
    } 
    pthread_mutex_unlock(&zone);
}
 
int main(void) {
    struct list *p = init();
    pthread_t td;
pthread_create(&td, NULL, (void *) push, (void *) p);
    pthread_join(td, NULL);
    return 0;
}