-
Notifications
You must be signed in to change notification settings - Fork 1
/
Oppg2.c~
49 lines (35 loc) · 895 Bytes
/
Oppg2.c~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//gcc -std=gnu99 -Wall -g -o Oppg2 Oppg2.c -lpthread
#include <pthread.h>
#include <stdio.h>
int i = 0;
int N = 100;
// Note the return type: void*
void* Inkrementer(void* lock){
for (int j = 0; j < N; j++){
pthread_mutex_lock(lock);
i+=2;
pthread_mutex_unlock(lock);
}
return NULL;
}
void* Dekrementer(void* lock){
for (int j = 0; j < N; j++){
pthread_mutex_lock(lock);
i--;
pthread_mutex_unlock(lock);
}
return NULL;
}
int main(){
pthread_mutex_t lock;
pthread_mutex_init(&lock,NULL);
pthread_t someThread;
pthread_t someThread2;
pthread_create(&someThread, NULL, Inkrementer,(void*) &lock);
pthread_create(&someThread2, NULL, Dekrementer,(void*) &lock);
pthread_join(someThread, NULL);
pthread_join(someThread2, NULL);
printf("%d\n", i);
pthread_mutex_destroy(&lock);
return 0;
}