#include /* simula un semaforo usando mutex e variabili condizione */ #include #include typedef struct { pthread_mutex_t mutex; pthread_cond_t cond; int counter; } mysem_t; mysem_t mysem; void unlock_mutex(void *m) { pthread_mutex_unlock((pthread_mutex_t *)m); } void mysem_init(mysem_t *s, int num) { pthread_mutexattr_t m; pthread_condattr_t c; s->counter = num; pthread_mutexattr_init(&m); pthread_mutex_init(&s->mutex, &m); pthread_mutexattr_destroy(&m); pthread_condattr_init(&c); pthread_cond_init(&s->cond, &c); pthread_condattr_destroy(&c); } void mysem_wait(mysem_t *s) { pthread_mutex_lock(&s->mutex); while (!(s->counter)) pthread_cond_wait(&s->cond, &s->mutex); s->counter--; pthread_mutex_unlock(&s->mutex); } void mysem_post(mysem_t *s) { pthread_mutex_lock(&s->mutex); if (!(s->counter++)) pthread_cond_signal(&s->cond); pthread_mutex_unlock(&s->mutex); } void *body(void *arg) { int i,j; mysem_wait(&mysem); for (j=0; j<40; j++) { for (i=0; i<1000000; i++); printf("%c",*(char *)arg); } mysem_post(&mysem); return NULL; } int main() { pthread_t t1,t2,t3; pthread_attr_t myattr; int err; mysem_init(&mysem,1); pthread_attr_init(&myattr); err = pthread_create(&t1, &myattr, body, (void *)"."); err = pthread_create(&t2, &myattr, body, (void *)"#"); err = pthread_create(&t3, &myattr, body, (void *)"o"); pthread_attr_destroy(&myattr); pthread_join(t1, NULL); pthread_join(t2, NULL); pthread_join(t3, NULL); printf("\n"); return 0; }