Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Deadlock
/****************************************************************************** Online C Compiler. Code, Compile, Run and Debug C program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <pthread.h> int qualquer_coisa; pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER; void* incrementar(void* num) { int i; pthread_mutex_lock(&mutex1); for(i = 0; i < 999999999; i++); pthread_mutex_lock(&mutex2); qualquer_coisa++; pthread_mutex_unlock(&mutex2); pthread_mutex_unlock(&mutex1); return 0; } void* decrementar(void* num) { int i; pthread_mutex_lock(&mutex2); for(i = 0; i < 999999999; i++); pthread_mutex_lock(&mutex1); qualquer_coisa--; pthread_mutex_unlock(&mutex1); pthread_mutex_unlock(&mutex2); 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
Add.c
tst2.c
Singly LinkedList.c
Taco Code
MyWall1
thread SleepWakeup and mutex
25/11/2021
lol
ex1
Hello world