回答編集履歴
1
yohhoyさんのアドバイスより、appleをatomicへ変更
test
CHANGED
@@ -15,6 +15,8 @@
|
|
15
15
|
#include <thread>
|
16
16
|
|
17
17
|
#include <mutex>
|
18
|
+
|
19
|
+
#include <atomic>
|
18
20
|
|
19
21
|
#include <chrono>
|
20
22
|
|
@@ -46,7 +48,7 @@
|
|
46
48
|
|
47
49
|
{
|
48
50
|
|
49
|
-
int apple=100;
|
51
|
+
std::atomic_int apple=100;
|
50
52
|
|
51
53
|
|
52
54
|
|
@@ -74,7 +76,7 @@
|
|
74
76
|
|
75
77
|
sleep(1); // りんご購入
|
76
78
|
|
77
|
-
int buy=std::min(5, apple);
|
79
|
+
int buy=std::min(5, static_cast<int>(apple));
|
78
80
|
|
79
81
|
apple -= buy;
|
80
82
|
|
@@ -114,7 +116,7 @@
|
|
114
116
|
|
115
117
|
sleep(2); // りんご購入
|
116
118
|
|
117
|
-
int buy=std::min(3, apple);
|
119
|
+
int buy=std::min(3, static_cast<int>(apple));
|
118
120
|
|
119
121
|
apple -= buy;
|
120
122
|
|
@@ -146,4 +148,4 @@
|
|
146
148
|
|
147
149
|
```
|
148
150
|
|
149
|
-
[wandbox](https://wandbox.org/permlink/
|
151
|
+
[wandbox](https://wandbox.org/permlink/DUUQxDhIJR7OoYvo)
|