Examples

Send + Sync

๋Œ€๋ถ€๋ถ„์˜ ํƒ€์ž…์€ Send + Sync์ž…๋‹ˆ๋‹ค:

  • i8, f32, bool, char, &str, โ€ฆ
  • (T1, T2), [T; N], &[T], struct { x: T }, โ€ฆ
  • String, Option<T>, Vec<T>, Box<T>, โ€ฆ
  • Arc<T>: atomic ์ฐธ์กฐ ์นด์šดํŠธ๋กœ ๋ช…์‹œ์  ์Šค๋ ˆ๋“œ-์„ธ์ดํ”„.
  • Mutex<T>: ๋‚ด๋ถ€ ์ž ๊ธˆ์„ ํ†ตํ•ด ๋ช…์‹œ์  ์Šค๋ ˆ๋“œ-์„ธ์ดํ”„.
  • AtomicBool, AtomicU8, โ€ฆ: ํŠน๋ณ„ํ•œ atomic ๋ช…๋ น์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

Most types you come across are Send + Sync:

  • i8, f32, bool, char, &str, โ€ฆ
  • (T1, T2), [T; N], &[T], struct { x: T }, โ€ฆ
  • String, Option<T>, Vec<T>, Box<T>, โ€ฆ
  • Arc<T>: Explicitly thread-safe via atomic reference count.
  • Mutex<T>: Explicitly thread-safe via internal locking.
  • AtomicBool, AtomicU8, โ€ฆ: Uses special atomic instructions.

์ œ๋„ˆ๋ฆญ ํƒ€์ž…์€ ์ผ๋ฐ˜์ ์œผ๋กœ ํƒ€์ž…ํŒŒ๋ผ๋ฉ”ํ„ฐ๊ฐ€ Send + Sync์ด๋ฉด Send + Sync ์ž…๋‹ˆ๋‹ค.

The generic types are typically Send + Sync when the type parameters are Send + Sync.

Send + !Sync

์•„๋ž˜ ํƒ€์ž…๋“ค์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋‚ด๋ถ€ ๊ฐ€๋ณ€์„ฑ์œผ๋กœ ์ธํ•ด ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๋กœ ์ด๋™๋  ์ˆ˜ ์žˆ์ง€๋งŒ ์Šค๋ ˆ๋“œ-์„ธ์ดํ”„ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

These types can be moved to other threads, but theyโ€™re not thread-safe. Typically because of interior mutability:

  • mpsc::Sender<T>
  • mpsc::Receiver<T>
  • Cell<T>
  • RefCell<T>

!Send + Sync

์•„๋ž˜ ํƒ€์ž…๋“ค์€ ์Šค๋ ˆ๋“œ-์„ธ์ดํ”„ ํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๋กœ ์ด๋™๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค:

  • MutexGuard<T>: OS๋ ˆ๋ฒจ์—์„œ ์ƒ์„ฑํ•œ ์Šค๋ ˆ๋“œ์—์„œ ํ• ๋‹นํ•ด์ œํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค

These types are thread-safe, but they cannot be moved to another thread:

  • MutexGuard<T>: Uses OS level primitives which must be deallocated on the thread which created them.

!Send + !Sync

์•„๋ž˜ ํƒ€์ž…๋“ค์€ ์Šค๋ ˆ๋“œ-์„ธ์ดํ”„ ํ•˜์ง€๋„ ์•Š๊ณ  ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๋กœ ์ด๋™๋  ์ˆ˜๋„ ์—†์Šต๋‹ˆ๋‹ค:

  • Rc<T>: ๊ฐ Rc<T> ๋Š” ๋น„ atomic ์ฐธ์กฐ ์นด์šดํŠธ๋ฅผ ํฌํ•จํ•˜๋Š” RcBox<T>๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.
  • *const T, *mut T: ๋Ÿฌ์ŠคํŠธ๋Š” raw ํฌ์ธํ„ฐ๊ฐ€ ํŠน๋ณ„ํ•œ ๋™์‹œ์„ฑ ๊ณ ๋ ค์‚ฌํ•ญ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.

These types are not thread-safe and cannot be moved to other threads:

  • Rc<T>: each Rc<T> has a reference to an RcBox<T>, which contains a non-atomic reference count.
  • *const T, *mut T: Rust assumes raw pointers may have special concurrency considerations.