考虑以下代码,我将每个线程附加到Vector,以便在我生成每个线程后将它们连接到主线程,但是我无法在我的向量上调用fn main() {
let requests = Arc::new(Mutex::new(Vec::new()));
let threads = Arc::new(Mutex::new(Vec::new()));
for _x in 0..100 {
println!("Spawning thread: {}", _x);
let mut client = Client::new();
let thread_items = requests.clone();
let handle = thread::spawn(move || {
for _y in 0..100 {
println!("Firing requests: {}", _y);
let start = time::precise_time_s();
let _res = client.get("http://jacob.uk.com")
.header(Connection::close())
.send().unwrap();
let end = time::precise_time_s();
thread_items.lock().unwrap().push((Request::new(end-start)));
}
});
threads.lock().unwrap().push((handle));
}
// src/main.rs:53:22: 53:30 error: type `alloc::arc::Arc<std::sync::mutex::Mutex<collections::vec::Vec<std::thread::JoinHandle<()>>>>` does not implement any method in scope named `unwrap`
for t in threads.iter(){
println!("Hello World");
}
}
JoinHandlers。
我该怎么做呢?
create PROCEDURE `p_provider_get_distributors_by_sort`(varOffset INT(11), varSortName varchar(50), varSortDirection varchar(6))
BEGIN
SET @st := concat('(SELECT id FROM distributor WHERE status = "AC") ORDER BY 'varSortName, varSortDirection' LIMIT 100 OFFSET ', varOffset);
PREPARE stmt FROM @st;
EXECUTE stmt;
END //
答案 0 :(得分:8)
首先,threads
中的Arc
不需要Mutex
。你可以保持Vec
:
let mut threads = Vec::new();
...
threads.push(handle);
这是因为你没有在线程之间共享threads
。您只能从主线程访问它。
其次,如果由于某种原因你做需要将它保留在Arc
中(例如,如果你的例子没有反映你的程序的实际结构更复杂),那么你需要锁定互斥锁以获取对包含向量的引用,就像在推送时一样:
for t in threads.lock().unwrap().iter() {
...
}