Browse Source

Added corrected main-race.c

Lorenz Bung 7 years ago
parent
commit
93732a171f
1 changed files with 5 additions and 0 deletions
  1. 5
    0
      hw8/simu1/main-race.c

+ 5
- 0
hw8/simu1/main-race.c View File

@@ -3,16 +3,21 @@
3 3
 #include "mythreads.h"
4 4
 
5 5
 int balance = 0;
6
+pthread_mutex_t mut;
6 7
 
7 8
 void* worker(void* arg) {
9
+    pthread_mutex_lock(&mut);
8 10
     balance++; // unprotected access
11
+    pthread_mutex_unlock(&mut);
9 12
     return NULL;
10 13
 }
11 14
 
12 15
 int main(int argc, char *argv[]) {
13 16
     pthread_t p;
14 17
     Pthread_create(&p, NULL, worker, NULL);
18
+    pthread_mutex_lock(&mut);
15 19
     balance++; // unprotected access
20
+    pthread_mutex_unlock(&mut);
16 21
     Pthread_join(p, NULL);
17 22
     return 0;
18 23
 }

Loading…
Cancel
Save