COdigo Mutex
Enviado por Farhid Cortés Zambra • 27 de Abril de 2019 • Documentos de Investigación • 559 Palabras (3 Páginas) • 108 Visitas
//Farhid Cortes Zambra
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
static int contador = 0;
static void * thread_1_function(void * arg);
static void * thread_2_function(void * arg);
static pthread_mutex_lock(pthread_mutex_t * mutex);
static pthread_mutex_unlock(pthread_mutex_t * mutex);
int main(){
pthread_t_thread_1, thread_2;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_create(&thread_1, NULL, *thread_1_function, NULL);
pthread_create(&thread_2, NULL, *thread_2_function, NULL);
pthread_join(thread_1, NULL);
pthread_join(thread_2, NULL);
printf("Valor contador : %d \n", contador);
return 0;
}
static void * thread_1_funcion(void * arg){
for(int i = 0;i<10000000;i++){
pthread_mutex lock(&mutex1);
contador+=1;
pthread_mutex_unlock(&mutex1);
}
}
static void * thread_2_funcion(void * arg){
for(int i = 0;i<10000000;i++){
pthread_mutex lock(&mutex1);
contador-=1;
pthread_mutex_unlock(&mutex1);
}
//Farhid Cortes Zambra
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
static int contador = 0;
static void * thread_1_function(void * arg);
static void * thread_2_function(void * arg);
static pthread_mutex_lock(pthread_mutex_t * mutex);
static pthread_mutex_unlock(pthread_mutex_t * mutex);
int main(){
pthread_t_thread_1, thread_2;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_create(&thread_1, NULL, *thread_1_function, NULL);
pthread_create(&thread_2, NULL, *thread_2_function, NULL);
pthread_join(thread_1, NULL);
pthread_join(thread_2, NULL);
printf("Valor contador : %d \n", contador);
return 0;
}
static void * thread_1_funcion(void * arg){
for(int i = 0;i<10000000;i++){
pthread_mutex lock(&mutex1);
contador+=1;
pthread_mutex_unlock(&mutex1);
}
}
static void * thread_2_funcion(void * arg){
for(int i = 0;i<10000000;i++){
pthread_mutex lock(&mutex1);
contador-=1;
pthread_mutex_unlock(&mutex1);
}
}
//Farhid Cortes Zambra
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
static int contador = 0;
static void * thread_1_function(void * arg);
static void * thread_2_function(void * arg);
static pthread_mutex_lock(pthread_mutex_t * mutex);
static pthread_mutex_unlock(pthread_mutex_t * mutex);
int main(){
pthread_t_thread_1, thread_2;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_create(&thread_1, NULL, *thread_1_function, NULL);
pthread_create(&thread_2, NULL, *thread_2_function, NULL);
pthread_join(thread_1, NULL);
pthread_join(thread_2, NULL);
printf("Valor contador : %d \n", contador);
return 0;
}
static void * thread_1_funcion(void * arg){
for(int i = 0;i<10000000;i++){
pthread_mutex lock(&mutex1);
contador+=1;
pthread_mutex_unlock(&mutex1);
}
}
static void * thread_2_funcion(void * arg){
for(int i = 0;i<10000000;i++){
pthread_mutex lock(&mutex1);
contador-=1;
pthread_mutex_unlock(&mutex1);
}
}//Farhid Cortes Zambra
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
static int contador = 0;
static void * thread_1_function(void * arg);
static void * thread_2_function(void * arg);
static pthread_mutex_lock(pthread_mutex_t * mutex);
static pthread_mutex_unlock(pthread_mutex_t * mutex);
int main(){
pthread_t_thread_1, thread_2;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_create(&thread_1, NULL, *thread_1_function, NULL);
pthread_create(&thread_2, NULL, *thread_2_function, NULL);
pthread_join(thread_1, NULL);
pthread_join(thread_2, NULL);
printf("Valor contador : %d \n", contador);
return 0;
}
static void * thread_1_funcion(void * arg){
for(int i = 0;i<10000000;i++){
pthread_mutex lock(&mutex1);
contador+=1;
pthread_mutex_unlock(&mutex1);
}
}
static void * thread_2_funcion(void * arg){
for(int i = 0;i<10000000;i++){
pthread_mutex lock(&mutex1);
contador-=1;
pthread_mutex_unlock(&mutex1);
}
}
//Farhid Cortes Zambra
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
static int contador = 0;
static void * thread_1_function(void * arg);
static void * thread_2_function(void * arg);
static pthread_mutex_lock(pthread_mutex_t * mutex);
static pthread_mutex_unlock(pthread_mutex_t * mutex);
int main(){
pthread_t_thread_1, thread_2;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_create(&thread_1, NULL, *thread_1_function, NULL);
pthread_create(&thread_2, NULL, *thread_2_function, NULL);
pthread_join(thread_1,
...