如何使用Condvar限制多线程? [英] How do I use a Condvar to limit multithreading?
问题描述
我正在尝试使用Condvar
来限制在任何给定时间处于活动状态的线程数.我很难找到有关如何使用Condvar
的良好示例.到目前为止,我有:
I'm trying to use a Condvar
to limit the number of threads that are active at any given time. I'm having a hard time finding good examples on how to use Condvar
. So far I have:
use std::sync::{Arc, Condvar, Mutex};
use std::thread;
fn main() {
let thread_count_arc = Arc::new((Mutex::new(0), Condvar::new()));
let mut i = 0;
while i < 100 {
let thread_count = thread_count_arc.clone();
thread::spawn(move || {
let &(ref num, ref cvar) = &*thread_count;
{
let mut start = num.lock().unwrap();
if *start >= 20 {
cvar.wait(start);
}
*start += 1;
}
println!("hello");
cvar.notify_one();
});
i += 1;
}
}
给出的编译器错误是:
error[E0382]: use of moved value: `start`
--> src/main.rs:16:18
|
14 | cvar.wait(start);
| ----- value moved here
15 | }
16 | *start += 1;
| ^^^^^ value used here after move
|
= note: move occurs because `start` has type `std::sync::MutexGuard<'_, i32>`, which does not implement the `Copy` trait
我完全不确定我对Condvar
的使用是否正确.我尝试尽可能地接近Rust API上的示例.难道是实现此目标的正确方法吗?
I'm entirely unsure if my use of Condvar
is correct. I tried staying as close as I could to the example on the Rust API. Wwat is the proper way to implement this?
推荐答案
以下是可编译的版本:
use std::{
sync::{Arc, Condvar, Mutex},
thread,
};
fn main() {
let thread_count_arc = Arc::new((Mutex::new(0u8), Condvar::new()));
let mut i = 0;
while i < 100 {
let thread_count = thread_count_arc.clone();
thread::spawn(move || {
let (num, cvar) = &*thread_count;
let mut start = cvar
.wait_while(num.lock().unwrap(), |start| *start >= 20)
.unwrap();
// Before Rust 1.42, use this:
//
// let mut start = num.lock().unwrap();
// while *start >= 20 {
// start = cvar.wait(start).unwrap()
// }
*start += 1;
println!("hello");
cvar.notify_one();
});
i += 1;
}
}
重要的部分可以从 Condvar::wait_while
或 Condvar::wait
:
The important part can be seen from the signature of Condvar::wait_while
or Condvar::wait
:
pub fn wait_while<'a, T, F>(
&self,
guard: MutexGuard<'a, T>,
condition: F
) -> LockResult<MutexGuard<'a, T>>
where
F: FnMut(&mut T) -> bool,
pub fn wait<'a, T>(
&self,
guard: MutexGuard<'a, T>
) -> LockResult<MutexGuard<'a, T>>
这表示wait_while
/wait
消费 guard
,这就是为什么您得到自己所犯的错误的原因-您不再拥有start
,因此无法调用任何方法都可以!
This says that wait_while
/ wait
consumes the guard
, which is why you get the error you did - you no longer own start
, so you can't call any methods on it!
这些函数在反映Condvar
的工作方式方面做得非常好-您暂时放弃对Mutex
(由start
表示)的锁定,当函数返回时,您将再次获得锁定
Theses functions are doing a great job of reflecting how Condvar
s work - you give up the lock on the Mutex
(represented by start
) for a while, and when the function returns you get the lock again.
解决方法是放弃锁,然后从wait_while
/wait
获取锁保护返回值.我还从if
切换到了while
,如
The fix is to give up the lock and then grab the lock guard return value from wait_while
/ wait
. I've also switched from an if
to a while
, as encouraged by huon.
这篇关于如何使用Condvar限制多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!