※ 引述《adpc ()》之銘言:
: 開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
: Linux
: 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
: 問題(Question):
: 兩個 Process 透為 Shared Memory 溝通時,
: 為了確定 Process 每一次都會去 Memory 抓最新的資料,
: 是否需要在 Shared Memory 中的 Data Structure 前面加上 volatile 關鍵字
: 以確保不會被 Compiler 的最佳化所影響。
: 餵入的資料(Input):
: 預期的正確結果(Expected Output):
: 錯誤結果(Wrong Output):
: 程式碼(Code):(請善用置底文網頁, 記得排版)
: 補充說明(Supplement):
這樣想好了
當compiler 看到下列的code
int A = 0
sleep(10)
if(A)
{
do something
}
在不知道有其他processes 會做更新的動作
一般都是直接把if 的部分直接優化掉
一般volatile 會用在io port 上也是這個原因.
compiler 不會知道你這個memory的位置是不是這一次讀取和下一次讀取都可以
保證是一樣的值.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.81.13.185