Ver Mensaje Individual
  #1 (permalink)  
Antiguo 04/12/2008, 00:40
mireis
 
Fecha de Ingreso: septiembre-2008
Mensajes: 22
Antigüedad: 15 años, 8 meses
Puntos: 0
mutex y condition variables pasadas por estructura

Mi duda es la siguiente: en el programa que adjunto... ¿por qué es necesario pasar el mutex y la condition variable dentro de la estructura buffer? ¿no se obtendría el mismo resultado inicializandolas de forma global?

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

struct buffer {
int num_consum, num_produc;
pthread_mutex_t m;
pthread_cond_t c;

int data[20];
}

int main(){
buffer mibuffer;
mibuffer.num_consum=0;
mibuffer.num_produc=0;
pthread_t t1, t2;

pthread_mutex_init (&mibuffer.m, NULL);
pthread_cond_init(&mibuffer.c, NULL);

pthread_create(&t1, NULL, ft1, &mibuffer);
pthread_create(&t2, NULL, ft2, &mibuffer);

pthread_join(t1, NULL);
pthread_join(t2, NULL);
}

void *ft1 (void*arg){
struct buffer *buffermio;
buffermio=(struct buffer*) arg;
for (i=40; i>20; i--){
pthread_mutex_lock(&buffermio->m);
buffermio->num_produced++;
buffermio->data[buffermio->num_produced-1]=i;
pthread_cond_signal(&buffermio->c);
pthread_mutex_unlock(&buffermio->m);
}
pthread_exit(NULL);
}

void *ft2(void*arg){
struct buffer *buffermio;
buffermio=(struct buffer*)arg;
for (i=0; i<20; i++){
pthread_mutex_lock(&buffermio->m);
buffermio->num_consum++;
pthread_mutex_unlock(&buffermio->m);
}
pthread_exit(NULL);
}