Skip to content

Instantly share code, notes, and snippets.

@afska
Last active August 29, 2015 14:17
Show Gist options
  • Save afska/7558dc792654fb81c601 to your computer and use it in GitHub Desktop.
Save afska/7558dc792654fb81c601 to your computer and use it in GitHub Desktop.
#include <pthread.h>
#include <stdio.h>
#include <semaphore.h>
int valor = 0;
pthread_mutex_t mutex;
void* decrementarValor(void* parametro) {
int i;
for (i = 0; i < 1000; i++) {
pthread_mutex_lock(&mutex);
valor--;
pthread_mutex_unlock(&mutex);
}
return 0;
}
int main(int argc, char **argv) {
pthread_mutex_init(&mutex, NULL);
pthread_t unHilo;
pthread_create(&unHilo, 0, decrementarValor, 0);
pthread_t otroHilo;
pthread_create(&otroHilo, 0, decrementarValor, 0);
pthread_join(unHilo, NULL);
pthread_join(otroHilo, NULL);
printf("%d", valor);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment