advance/concurrency-with-threads/sync1 #736
Replies: 25 comments 29 replies
-
我点了一下playground,然后刷屏了。zz |
Beta Was this translation helpful? Give feedback.
-
这一行 |
Beta Was this translation helpful? Give feedback.
-
可否添加parking_lot的对比 |
Beta Was this translation helpful? Give feedback.
-
Condvar 例子的代码,没看懂执行顺序 👀 |
Beta Was this translation helpful? Give feedback.
-
let hdl = thread::spawn(move || {
condvar的代码稍微简化了一下,觉得会更清晰些。 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
CondVar 交替打印 use std::sync::{Arc, Condvar, Mutex};
use std::thread;
#[derive(Clone, Debug)]
enum Next {
Child,
Main,
}
let next = Arc::new(Mutex::new(Next::Main));
let cond = Arc::new(Condvar::new());
let next2 = next.clone();
let cond2 = cond.clone();
let handle = thread::spawn(move || {
let mut lock = next2.lock().unwrap();
let mut next_flag = (*lock).clone();
drop(lock);
for i in 1..=3 {
while let Next::Main = next_flag {
next_flag = (*cond2.wait(next2.lock().unwrap()).unwrap()).clone();
} // next_flag 为 Next::Child 时跳出 while-loop
eprintln!("child:\t{}", i);
next_flag= Next::Main;
*next2.lock().unwrap() = next_flag.clone();// 下一个进行打印的是main线程
}
});
for i in 1..=3 {
eprintln!("main:\t{}", i);
let mut next_flag = next.lock().unwrap();
*next_flag = Next::Child; // 下一个进行打印的是child线程
drop(next_flag);
cond.notify_one();
thread::sleep(Duration::from_secs(1));
//睡一秒, 给child线程提供上锁的机会.
}
handle.join().unwrap(); 输出: main: 1
child: 1
main: 2
child: 2
main: 3
child: 3 |
Beta Was this translation helpful? Give feedback.
-
互斥锁是不是相当于数量为1的信号量 |
Beta Was this translation helpful? Give feedback.
-
Condvar的这一块代码没大懂 {
m = false;
*cflag.lock().unwrap() = false;
} *cflag.lock().unwrap() = false; 感觉不需要这一行,因为 m 已经是可变引用了,我再ide试了下结果也是正常的,有大神解答下吗 |
Beta Was this translation helpful? Give feedback.
-
请教下面的问题(注释中):
|
Beta Was this translation helpful? Give feedback.
-
多线程死锁加这个循环的代码 |
Beta Was this translation helpful? Give feedback.
-
i wanna try unsafe concurrent progame but this result always be zero, I don't why :( use std::sync::{Arc, Mutex}; fn main() {
} |
Beta Was this translation helpful? Give feedback.
-
为什么condvar的例子中注释了sleep语句会程序运行错误? |
Beta Was this translation helpful? Give feedback.
-
condvar章节的while !*lock的!是啥东西 |
Beta Was this translation helpful? Give feedback.
-
用条件变量(Condvar)控制线程的同步 ,主线程这里获取了锁*flag.lock().unwrap();子线程clone的变量又去获取锁c_flag.lock().unwrap(); 这里不是造成死锁吗,谁能解答一下 |
Beta Was this translation helpful? Give feedback.
-
安装依赖 |
Beta Was this translation helpful? Give feedback.
-
condvar例子中为什么要这么写?
我把代码修改为:去掉while循环, 注释掉flag cflag的修改
|
Beta Was this translation helpful? Give feedback.
-
温馨提示: 自从rust 1.7.0版本后, Semaphore 就被从标准库中移除了 |
Beta Was this translation helpful? Give feedback.
-
2024年6月,测试rust std sync 的rwlock与mutex锁与 spin 的自旋锁速度相当。 |
Beta Was this translation helpful? Give feedback.
-
此处建议加一下虚假唤醒的教程,不然的话无法解释这段代码
|
Beta Was this translation helpful? Give feedback.
-
这个例子感觉也不是很好啊, 想要正常输出, 主线程就必须休息阻塞一会:
这个时间控制的太随意了吧, 控制时间短了影响子线程的运行, 主线程一直阻塞, 时间长了有影响性能 有没有大佬有其他解决办法呢? |
Beta Was this translation helpful? Give feedback.
-
自己用Condvar和Mutex实现的一个信号量: use std::sync::{Arc, Condvar, Mutex};
pub struct Semaphore {
count: Mutex<u64>,
cv: Condvar,
}
impl Semaphore {
pub fn new(count: u64) -> Self {
Self {
count: Mutex::new(count),
cv: Condvar::new(),
}
}
pub fn arc(count: u64) -> Arc<Self> {
Arc::new(Self::new(count))
}
pub fn acquire(&self) -> SemGuard {
let mut guard = self
.cv
.wait_while(self.count.lock().unwrap(), |count| *count == 0)
.unwrap();
*guard -= 1;
SemGuard { sem: self }
}
fn release(&self) {
let mut guard = self.count.lock().unwrap();
*guard += 1;
self.cv.notify_one();
}
}
pub struct SemGuard<'a> {
sem: &'a Semaphore,
}
impl<'a> Drop for SemGuard<'a> {
fn drop(&mut self) {
self.sem.release();
}
} |
Beta Was this translation helpful? Give feedback.
-
用Mutex和Condvar实现的一个Barrier: use std::sync::{Arc, Condvar, Mutex};
pub struct Barrier {
count: Mutex<State>,
cv: Condvar,
total: u64,
}
struct State {
count: u64,
generation: u64,
}
impl Barrier {
pub fn new(count: u64) -> Self {
Self {
count: Mutex::new(State {
count,
generation: 0,
}),
cv: Condvar::new(),
total: count,
}
}
pub fn arc(count: u64) -> Arc<Self> {
Arc::new(Self::new(count))
}
pub fn wait(&self) {
let mut state = self.count.lock().unwrap();
let current_generation = state.generation;
state.count -= 1;
if state.count == 0 {
state.count = self.total;
state.generation += 1;
self.cv.notify_all();
} else {
while current_generation == state.generation {
state = self.cv.wait(state).unwrap();
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
标准库中的信号量不再推荐使用写了原因:总结起来是不是就是"信号量没什么卵用" |
Beta Was this translation helpful? Give feedback.
-
advance/concurrency-with-threads/sync1
https://course.rs/advance/concurrency-with-threads/sync1.html
Beta Was this translation helpful? Give feedback.
All reactions