Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <pthread.h>
- pthread_mutex_t mutex;
- int counter = 0;
- void odejmowanie() {
- pthread_mutex_lock(&mutex);
- for(int i = 0; i < 100000; i++) {
- counter--;
- printf("%d ", counter);
- }
- pthread_mutex_unlock(&mutex);
- }
- void dodawanie() {
- pthread_mutex_lock(&mutex);
- for (int i = 0; i < 100000; i++) {
- counter++;
- printf("%d ", counter);
- }
- pthread_mutex_unlock(&mutex);
- }
- void main() {
- pthread_t dod, odej;
- pthread_mutex_init(&mutex, 0);
- int result;
- pthread_create(&dod, NULL, dodawanie, NULL);
- pthread_create(&odej, NULL, odejmowanie, NULL);
- pthread_join(dod, &result);
- pthread_join(odej, &result);
- pthread_detach(&dod);
- pthread_detach(&odej);
- printf("%d ", counter);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement