Shared State

๋Ÿฌ์ŠคํŠธ๋Š” ์ฃผ๋กœ ์•„๋ž˜ ๋‘ ๊ฐ€์ง€ ํƒ€์ž… ์‹œ์Šคํ…œ์„ ์ด์šฉํ•ด์„œ ๊ณต์œ  ๋ฐ์ดํ„ฐ ๋™๊ธฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค:

Rust uses the type system to enforce synchronization of shared data. This is primarily done via two types:

  • Arc<T>, atomic1 ์ฐธ์กฐ ์นด์šดํŠธ T: ์Šค๋ ˆ๋“œ ์‚ฌ์ด์˜ ๊ณต์œ ๋ฅผ ๋‹ด๋‹นํ•˜๊ณ , ๋งˆ์ง€๋ง‰ ์Šค๋ ˆ๋“œ ์ข…๋ฃŒ์‹œ T๋ฅผ ํ•ด์ œํ•ฉ๋‹ˆ๋‹ค.
  • Mutex<T>: T๊ฐ’์— ๋Œ€ํ•œ ์ƒํ˜ธ๋ฐฐ์ œ ์—‘์„ธ์Šค๋ฅผ ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.
  • Arc<T>, atomic reference counted T: handled sharing between threads and takes care to deallocate T when the last thread exits,
  • Mutex<T>: ensures mutual exclusion access to the T value.

์—ญ์ฃผ

1

C++์˜ atomic์„ ๊ตฌํ˜„ํ–ˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์›์ž์  ์—ฐ์‚ฐ(ํ•œ๋ฒˆ์— ์ผ์–ด๋‚˜๋Š” ๋ช…๋ น์–ด ์—ฐ์‚ฐ)์œผ๋กœ 1๊ฐœ์˜ ๋ช…๋ น์–ด๋กœ ์ฒ˜๋ฆฌํ–ˆ๋‹ค/์•ˆํ–ˆ๋‹ค๋กœ๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.