advance/concurrency-with-threads/send-sync #763
Replies: 9 comments 20 replies
-
unsafe impl Sync for MyBox {} |
Beta Was this translation helpful? Give feedback.
-
若类型 T 的引用&T是Send,则T是Sync。 这个结论感觉有点绕,能帮忙解释一下吗? |
Beta Was this translation helpful? Give feedback.
-
有个关于裸指针转换的问题 let p = 6 as *const i32;
unsafe{
let n = *p;
println!("{}", n);
} 上面这段代码是无法运行的 |
Beta Was this translation helpful? Give feedback.
-
请教下,实现Sync智能指针的那部分代码为什么这样实现会在Mutex::new的地方报错 borrowed value does not live long enough let b = MyBox(5 as *const u8);
let v = Arc::new(Mutex::new(&b));
let t = thread::spawn(move || {
let _v1 = v.lock().unwrap();
});
t.join().unwrap(); 感觉Mutex::new接收的参数类型都是&MyBox啊 |
Beta Was this translation helpful? Give feedback.
-
b是一个临时变量,v智能指针持有该变量的引用的话,&b的生命周期不够长啊。
…------------------ 原始邮件 ------------------
发件人: ***@***.***>;
发送时间: 2023年2月9日(星期四) 凌晨0:36
收件人: ***@***.***>;
抄送: ***@***.***>; ***@***.***>;
主题: Re: [sunface/rust-course] advance/concurrency-with-threads/send-sync (Discussion #763)
请教下,实现Sync智能指针的那部分代码为什么这样实现会在Mutex::new的地方报错 borrowed value does not live long enough
let b = MyBox(5 as *const u8); let v = Arc::new(Mutex::new(&b)); let t = thread::spawn(move || { let _v1 = v.lock().unwrap(); }); t.join().unwrap();
感觉Mutex::new接收的参数类型都是&MyBox啊
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
use std::thread;
use std::time::Duration;
fn main() {
let new_thread = thread::spawn(move || {
thread::spawn(move || loop {
println!("I am a new thread.");
thread::sleep(Duration::from_millis(1));
});
thread::spawn(move || loop {
println!("I am b new thread.");
thread::sleep(Duration::from_millis(1));
});
thread::spawn(move || loop {
println!("I am c new thread.");
thread::sleep(Duration::from_millis(1));
});
thread::spawn(move || loop {
println!("I am d new thread.");
thread::sleep(Duration::from_millis(1));
});
});
new_thread.join().unwrap();
thread::sleep(Duration::from_secs(100));
} 四核的机器(17款 MBP)这样写, cpu 使用率确实上来了, 但是每个核都没到 100%, 只有 50~60, 为什么? |
Beta Was this translation helpful? Give feedback.
-
复合类型中有一个成员没实现Send,该复合类型就不是Send,因此我们需要手动为它实现: #[derive(Debug)]
struct MyBox(*mut u8);
unsafe impl Send for MyBox {} 这里MyBox(mut u8)是复合类型吗?请问如何理解mut u8没有实现Send,而为MyBox 实现Send就足够了? |
Beta Was this translation helpful? Give feedback.
-
这里是不是写反了,应该是 |
Beta Was this translation helpful? Give feedback.
-
求教一下,我现在想实现这样一个功能:生成n个线程,并且将一个数组里的n个元素分别传入n个线程,该怎么用代码实现?伪代码大致如下: use std::thread;
struct T {
a: i32,
b: String,
// some other fields that implements Send and Sync
}
const N: usize = 5;
fn main() {
thread::scope(move |s| {
let array: [Box<T>; N];
// some code that initialize `array` at run time
for i in 0..N {
thread::spawn(move || {
// take ownership of i
let a = array[i];
// Some operations to `a`
});
};
});
}
|
Beta Was this translation helpful? Give feedback.
-
advance/concurrency-with-threads/send-sync
https://course.rs/advance/concurrency-with-threads/send-sync.html
Beta Was this translation helpful? Give feedback.
All reactions