Rust ์ฑ ์์๋ ์์ ํฌ์ธํฐ๊ฐ "์ ์ก ๊ฐ๋ฅํ ๊ฒ์ผ๋ก ๊ฐ์ฃผ๋ฉ๋๋ค(๋ด์ฉ์ด ์ ์ก ๊ฐ๋ฅํ ๊ฒ์ผ๋ก ๊ฐ์ฃผ๋๋ ๊ฒฝ์ฐ)"๋ผ๊ณ ๋งํฉ๋๋ค.
๊ทธ๋ฌ๋ ๋ด๊ฐ ๋ณผ ์ ์๋ ๋ฐ์ ๋ฐ๋ฅด๋ฉด ์ปดํ์ผ๋ฌ๋ Sendable ์ ํ์ ๋ํ ์์ ํฌ์ธํฐ๋ฅผ ๋ง๋๋ฉด the trait
core::marker::Send is not implemented for the type
์ค๋ฅ๋ฅผ ์์ฑํฉ๋๋ค.
๋ด๊ฐ ๋ญ๊ฐ ์๋ชปํ๊ณ ์์ ์๋ ์๊ณ , ๋ฌธ์๊ฐ ๋ถ๋ถ๋ช ํ ์๋ ์๊ณ , ์ด ์ฝ๋์ ์ค์ ๋ฌธ์ ๊ฐ ๋ฌด์์ด๋ ๊ฐ์ ์ค๋ฅ ๋ฉ์์ง๊ฐ ๋์์ด ๋์ง ์์ ์๋ ์์ต๋๋ค.
extern crate core;
use std::thread::Thread;
struct ShouldBeSendable {
x: i32
}
unsafe impl core::marker::Send for ShouldBeSendable { }
fn main() {
let sendablePtr : *const ShouldBeSendable = &ShouldBeSendable {x: 5};
let closure = move |:| {
*sendablePtr;
};
let something = Thread::spawn(closure);
}
๋ค์๊ณผ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
test.rs:15:21: 15:34 error: the trait `core::marker::Send` is not implemented for the type `*const ShouldBeSendable` [E0277]
test.rs:15 let something = Thread::spawn(closure);
^~~~~~~~~~~~~
test.rs:15:21: 15:34 note: `*const ShouldBeSendable` cannot be sent between threads safely
test.rs:15 let something = Thread::spawn(closure);
^~~~~~~~~~~~~
error: aborting due to previous error
$ rustc --version
rustc 1.0.0-dev (d15192317 2015-01-25 16:09:48 +0000)
์ด๊ฒ์ ์ต๊ทผ์ ๋ณ๊ฒฝ๋์ด ๋ฌธ์๊ฐ ์ต์ ์ํ๊ฐ ์๋๋๋ค. ๋ฒ๊ทธ๋ฅผ ์์์ฐจ๋ฆฌ๊ณ ์ ๊ณ ํด ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค!
@drewcrawford Unique
๋ํผ๋ฅผ ์ฌ์ฉํ์ฌ ์์ ํฌ์ธํฐ๋ฅผ Send
๊ฐ๋ฅํ๊ฒ ๋ง๋ค ์ ์์ต๋๋ค. ์ด ์์ ๋ ๋ฒ์ ์ ์ฝ๋๊ฐ ์๋ํฉ๋๋ค.
use std::ptr::Unique;
use std::thread::Thread;
#[derive(Copy)]
struct ShouldBeSendable {
x: i32
}
unsafe impl std::marker::Send for ShouldBeSendable { }
fn main() {
let ptr : *mut ShouldBeSendable = &mut ShouldBeSendable {x: 5}; // this is not `Send`
let sendablePtr = Unique(ptr); // but this is!
let closure = move |:| {
// `sendablePtr` con be moved inside this closure
let ptr = sendablePtr.0; // unpack the raw pointer
println!("{}", unsafe { (*ptr).x })
};
let something = Thread::scoped(closure).join();
}
NonNull
์ฌ์ฉํ๋ Rust 1.25์ @japaric ์์ .
https://play.rust-lang.org/?gist=1ce2532a0eefc60695663c26faddebe1&version=stable
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
NonNull
์ฌ์ฉํ๋ Rust 1.25์ @japaric ์์ .https://play.rust-lang.org/?gist=1ce2532a0eefc60695663c26faddebe1&version=stable