在前面,我们使用多线程的时候,因为所有权的原因,我们把相关变量都通过clone复制了一遍,这肯定不是好办法,经过查找,发现rust提供了一个Arc智能指针的东西,可以使资源被多个所有者共享。先看代码
fn main() {
let file_path="D:/uploadBigTest.txt";
let mut file=std::fs::File::open(file_path).unwrap();
let file_size=file.metadata().unwrap().len();
let mut buf=Vec::new();
file.read_to_end(&mut buf);
let part_size=10*1024*1024;
let part_num=if file_size%part_size==0{
file_size/part_size
}else{
file_size/part_size+1
};
let client=Arc::new(reqwest::blocking::Client::new());
let response=client.post("http://localhost:8234/group2/big/upload/")
.header("Upload-Length",file_size)
.header("Tus-Resumable","1.0.0")
.send()
.unwrap();
let mut file_location=Arc::new(response.headers().get("Location").unwrap().to_str().unwrap().to_string());
for i in 0..part_num {
let start= (i * part_size) as usize;
let mut end=((i+1)*part_size) as usize;
if end>(file_size as usize){
end=file_size as usize;
}
let upload_url=file_location.clone();
let body=(&buf[start..end]).to_vec();
let http_client=client.clone();
thread::spawn(move ||{
let response=http_client.patch(upload_url.as_str())
.header("Content-Type","application/offset+octet-stream")
.header("Tus-Resumable","1.0.0")
.header("Upload-Offset",start)
.body(body).send().unwrap();
println!("part {} upload success",i);
}).join();
}
}
和之前的代码对比的话,会发现file_location和client两个变量都变成了Arc,使用方式如下:
let client=Arc::new(reqwest::blocking::Client::new());
但仔细观察的话,在循环体里面,我们还是对client和file_location两个变量执行了clone方法,但此clone非彼clone,arc执行clone,只是对计数器进行了+1的操作,相当于多了一个引用,并没有对堆上的值进行深度复制,类似于java的回收机制,当还有引用的时候就不进行回收了,只不过rust是在编译器进行处理的。我们通过下面代码验证下:
let arc_test=Arc::new(Mutex::new(vec![]));
for i in 0..5{
let arc_clone=arc_test.clone();
thread::spawn(move||arc_clone.lock().unwrap().push(i)).join();
}
println!("{:?}",arc_test);
我们看执行会发现arc_test里面已经包含了0、1、2、3、4五个元素,说明arc_clone和arc_test其实是同一个对象,所以可以证明,arc的clone方法只是对资源的引用。
另外,上面代码还用到了Mutex这个东西,他是rust互斥锁的实现,类似于java的synchronized 和ReentrantLock
上面都是针对多线程来讲的,rust还提供了一个RC的指针,是在单线程下使用的,使用方法和Arc类似,就不再详细写了。
网友评论