浏览代码

Added corrected main-race.c

Lorenz Bung 7 年前
父节点
当前提交
93732a171f
共有 1 个文件被更改,包括 5 次插入0 次删除
  1. 5
    0
      hw8/simu1/main-race.c

+ 5
- 0
hw8/simu1/main-race.c 查看文件

@@ -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
 }

正在加载...
取消
保存