20. int x{0};
int fun() {
auto y = x;
y = x;
x = 10;
x = 20;
return y;
}
⼿手元の msvc14.0 (/O2 /volatile:iso option)で assymbly-out すると
こんな感じ (fun のとこだけ)
?fun@@YAHXZ PROC ; fun, COMDAT
; Line 5
mov eax, DWORD PTR ?x@@3HA ; x
; Line 6
mov DWORD PTR ?x@@3HA, 20 ; x
; Line 8
ret 0
?fun@@YAHXZ ENDP ; fun
30. xx をを volatile intvolatile int →→ std::atomic< int >std::atomic< int > に変えるに変える
まずは愚直に…
std::atomic< int > x{0};
int fun() {
auto y = x; // error!! copy ctor is deleted
y = x; // error!! copy assign is deleted
x = 10;
x = 20;
return y;
}
http://melpon.org/wandbox/permlink/ld7zvejwv3KAahkn
atomic の copy ctor/assign が deleted のため compile error
31. 修正する
std::atomic< int > x{0};
int fun() {
// auto y = x; // error!! copy ctor is deleted
int reg = x.load();
std::atomic< int > y{ reg };
// y = x; // error!! copy assign is deleted
reg = x.load();
y = reg;
x = 10;
x = 20;
return y;
}
http://melpon.org/wandbox/permlink/W2dqqMZ0KLXyplVQ
32. ?fun@@YAHXZ PROC ; fun, COMDAT
; File
; Line 8
mov eax, DWORD PTR ?x@@3U?$atomic@H@std@@A ; x
; Line 9
mov DWORD PTR y$[rsp], eax
; Line 10
mov eax, DWORD PTR ?x@@3U?$atomic@H@std@@A ; x
; Line 11
xchg DWORD PTR y$[rsp], eax
; Line 12
mov eax, 10
; Line 13
mov ecx, 20
xchg DWORD PTR ?x@@3U?$atomic@H@std@@A, eax ; x
xchg DWORD PTR ?x@@3U?$atomic@H@std@@A, ecx ; x
; Line 14
mov eax, DWORD PTR y$[rsp]
; Line 15
ret 0
?fun@@YAHXZ ENDP ; fun
((・・__・・;);) あれあれ????
33. テキスト本⽂文テキスト本⽂文
std::atomic< int > y(x.load());
x = 20;
になるはずとか⾔言ってるけどならないになるはずとか⾔言ってるけどならない……
(clang3.8/gcc5.2(clang3.8/gcc5.2 でも同様でも同様))
注注: y: y ののatomicatomic なな copycopy は怪しいのでそこを省いて、は怪しいのでそこを省いて、 yy なしにしてもならなかったなしにしてもならなかった