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 areSend + 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>
: eachRc<T>
has a reference to anRcBox<T>
, which contains a non-atomic reference count.*const T
,*mut T
: Rust assumes raw pointers may have special concurrency considerations.