- 论坛徽章:
- 59
|
回复 1# zylthinking
lkp 在不是 volatile 的情况下, 是不是不安全?
yes, it is unsafe.
关键是如果 *(lkp) = 0; 已经进入了 load/store unit, 则可以保证 ++n 至少在 load/store unit 内, 或者已经到了寄存器中???
另外, 如果另一个线程读 n 时, 产生的汇编应该是从内存读, 那么它怎么知道内存中某单元中的值已经在 load/store unit 中了?
似乎首先要确认的是 ++n 的结果在 load/store unit 中时, 写的目标到底是存储器还是寄存器;
如果是寄存器, 也就是 n 被优化到寄存器中了, 那么CPU 还能直接从 load/store unit 中读吗? 但如果不从其中读而直接读内存, 那读出的就是一个错误的值
if you define the lkp as a volatile value, it is not still a promble.
this code has no problem.
to **:
3。我猜 不会当作 volatile 来对待
在不知道为 volatile 时就已经编译好了
i agree.
但link时应当报错,因为 volatile int n 和 int n 不应该弄成一样的名字
i am not agree with it.
"int n" or "volatile int n" has the same symbol name (both are "_n") in c.
but it is not the same in c++ (with type prefix);
|
|