Last active
August 29, 2015 14:17
-
-
Save afska/7558dc792654fb81c601 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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