回答編集履歴
1
追記
test
CHANGED
@@ -53,3 +53,53 @@
|
|
53
53
|
|
54
54
|
|
55
55
|
都合11回 lockしてます。
|
56
|
+
|
57
|
+
正しくは:
|
58
|
+
|
59
|
+
```C
|
60
|
+
|
61
|
+
void *calc(void *arg)
|
62
|
+
|
63
|
+
{
|
64
|
+
|
65
|
+
mythread_args_t *targs = (mythread_args_t *) arg;
|
66
|
+
|
67
|
+
int prev;
|
68
|
+
|
69
|
+
int i;
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
for(i=0;i<10;i++){
|
74
|
+
|
75
|
+
pthread_mutex_lock(targs->mlock); /* ココから */
|
76
|
+
|
77
|
+
prev = count;
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
sleep(rand() % 5);
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
count = prev + 1;
|
86
|
+
|
87
|
+
pthread_mutex_unlock(targs->mlock); /* ココまで排他 */
|
88
|
+
|
89
|
+
printf("[%s] %d -> %d\n", (char *)arg, prev, count);
|
90
|
+
|
91
|
+
sleep(rand() % 5);
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
pthread_exit(NULL);
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
```
|
104
|
+
|
105
|
+
ってとこかしら。
|