volatile

๊ธฐ๋ณธ์ ์ธ ๋ฉํฐ ํ๋ก์ธ์ค์ ๊ตฌ์กฐ.
Core(CPU) ๋ณ๋ก, ์บ์๋ค์ ๊ฐ์ง๊ณ ์๊ณ , L3 ๋ฅผ ๊ฑฐ์ณ RAM์ผ๋ก ๊ฐ๋ ๊ตฌ์กฐ.
๋ง์ฝ์ L1 cache์์ A ๋ฉ๋ชจ๋ฆฌ์ ๊ฐ์ ์ ๋ฐ์ดํธ๊ฐ ์ผ์ด๋๋ฉด ์ด๋ป๊ฒ ๋ ๊น?
๊ทธ ์ ๋ฐ์ดํธ๊ฐ RAM์ ์ ํ๋ ๋ ๊น์ง๋ ๋ค๋ฅธ ์ฝ์ด์์๋ ์ ์ ์์. (์ ๋ฐ์ดํธ๊ฐ ์๋์์ผ๋)
data inconsistency
์ด ํค์๋๋ฅผ ์ฌ์ฉํ๋ฉด, ํด๋น ๋ณ์์ ๋ณํ๊ฐ main memory๊น์ง ๋ฐ๋ก ์ ํ๋จ์ ์๋ฏธ. ([[compare-and-swap]] ๋ผ๋ ๋ฐฉ๋ฒ์ ์ด์ฉํด์)
Memory Visibility
์ค๋ ๋๋ 2๊ฐ main, runner thread
main thread์์ ์ ๋ฐ์ดํธ ํ ๋ณ์ (number, ready) ๊ฐ runner thread ์๊ฒ๋ ๋ฐ๋ก ์ฝ์ ์ ์์๊น?
๊ทธ๋ด ์๋, ์๊ฐ์ด ๊ฑธ๋ฆด ์๋, ํน์ ์ ๋ฐ์ดํธ๊ฐ ์๋ ์๋ ์์.
์?
processors tend to queue those writes in a special write buffer๋ฐ๋ก ์ ์ฉ๋๋ ๊ฒ์ด ์๋, write buffer์ queue ๋ฅผ ๋์ ธ์, ์ผ์ ์๊ฐ์ด ์ง๋ ํ์ main memory์ ๋ฐ์๋๊ธฐ ๋๋ฌธ
์ฆ ์ ๋ฆฌํ์๋ฉด,
main thread๊ฐ number, ready ๋ณ์๋ฅผ ์ ๋ฐ์ดํธ ํ์ ๋, reader thread๊ฐ ์ ๋ฐ์ดํธ ๋ ๋ณ์๋ฅผ ์ฝ๋ ๋ค๋ ๋ณด์ฅ์ด ์์.
์ฌ์ง์ด programe order (number -> ready) ์์ผ๋ก ์ ๋ฐ์ดํธํด์ ๋ณด๊ธฐ ๋ณด๋ค๋ ์์ ๋ค๋ฅธ ์์ (ready -> number) ์์ผ๋ก๋ ๋ณผ ์ ์์. (๊ฐ๋ฐ์์ ์๋์๋ ์์ ๋ค๋ฅด๊ฒ..)
ready = true๋ก ๋ฐ๋์์ง๋ง number๋ ์ ๋ฐ์ดํธ ๋์ง ์์ ์ฑ 0์ ํ๋ฆฐํธ ํ๊ณ ์ข ๋ฃ๋ ์๋ ์๋ค๋ ๋ป์ธ๋ฏ.
To ensure programm order
To ensure that updates to variables propagate predictably to other thread
ํ๋ก๊ทธ๋จ์ ์์ฑํ ์์๋๋ก ๋ณ์๋ค์ด ์ ๋ฐ์ดํธ ๋๋ ๊ฒ์ ๋ณด์ฅํ๊ธฐ ์ํด์ ?
volatile๋ณ์๋ฅผ ์ฌ์ฉ.
์ด๋ ๊ฒ ํ๋ฉด.. number ์ ๋ฐ์ดํธ ํ์ ready๊ฐ ์ ๋ฐ์ดํธ ๋๋ค.
42 print ํ ์ข ๋ฃ๋ฅผ ์์ธกํ ์ ์์๋ฏ.
For multithreaded applications
์ผ๊ด์ ์ธ ํ๋์ ์ํด์ 2๊ฐ์ง ๋ฃฐ์ ์ง์ผ์ผํจ.
Mutual Exclusion : ์น๋ช ์ ์ธ ๋ถ๋ถ์๋ ํ๋ฒ์ ํ๋์ ์ค๋ ๋๋ง ์คํํ๋๋ก.
Visibility : ๊ณต์ ๋ ์์์ ํ๋์ ์ค๋ ๋๋ก ๋ถํฐ ๋ง๋ค์ด์ง ๋ณํ๋ค์, ๋ค๋ฅธ ์ค๋ ๋๋ค์ด ๋ณผ ์ ์์ด์ผํจ. ์? data consistency ๋ฅผ ์ ์งํ๊ธฐ ์ํด
[[sy]] ๋ฉ์๋๋ 1,2 ๋ฒ์ ๋ฃฐ์ ๋ชจ๋ ๋ง์กฑ ์ํฌ ์ ์์.
๋ค๋ง ์๋นํ ์์์ ์๋ชจ.
๊ทธ๋ ์ง๋ง [[volatile]] ๋ mutual exclusion ์์ด 2๋ฒ visibility ์ธก๋ฉด์ ๋ง์กฑ์ํด.
์ฐธ๊ณ
Last updated
Was this helpful?