Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Mutex Safe
#include <stdio.h> #include <stdlib.h> #include <pthread.h> int qualquer_coisa; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; void* incrementar(void* num) { int x, i; pthread_mutex_lock(&mutex); for (x = 0; x < 3; x++) { int temp = qualquer_coisa; for(i = 0; i < 10000; i++); qualquer_coisa = temp + 1; } pthread_mutex_unlock(&mutex); return 0; } void* decrementar(void* num) { int x, i; pthread_mutex_lock(&mutex); for (x = 0; x < 3; x++) { int temp = qualquer_coisa; for(i = 0; i < 10000; i++); qualquer_coisa = temp - 1; } pthread_mutex_unlock(&mutex); return 0; } int main() { int num_th1, num_th2; pthread_t th1, th2; qualquer_coisa = 10; pthread_create(&th1, NULL, incrementar, (void*) &num_th1); pthread_create(&th2, NULL, decrementar, (void*) &num_th2); pthread_join(th1, NULL); pthread_join(th2, NULL); printf("Resultado %d \n", qualquer_coisa); return 0; }
run
|
edit
|
history
|
help
0
Rotación matriz anti horaria
Null deref
Perfect number
Spring 2017 Project 2 v1.1
Float ispis
address extraction
Char data type.c
CheckProcessorEndianness
KN king Array ques
141122_PI6