网站建设的售后,装修公司网站 源码,wordpress 分类 文章数,三点水网站建设合同文章目录 Rust对多线程的支持std::thread::spawn创建线程线程与 move 闭包 使用消息传递在线程间传送数据std::sync::mpsc::channel()for received in rx接收两个producer 共享状态并发std::sync::Mutex在多个线程间共享Mutex#xff0c;使用std::sync::Arc 参考 Rust对多线程… 文章目录 Rust对多线程的支持std::thread::spawn创建线程线程与 move 闭包 使用消息传递在线程间传送数据std::sync::mpsc::channel()for received in rx接收两个producer 共享状态并发std::sync::Mutex在多个线程间共享Mutex使用std::sync::Arc 参考 Rust对多线程的支持
rust默认仅支持一对一线程也就是说操作系统线程。
可以引入crate包来使用绿色线程。
std::thread::spawn创建线程
use std::thread;
use std::time::Duration;fn main() {let handle thread::spawn(|| {for i in 1..10 {println!(hi number {} from the spawned thread!, i);thread::sleep(Duration::from_millis(1));}});for i in 1..5 {println!(hi number {} from the main thread!, i);thread::sleep(Duration::from_millis(1));}handle.join().unwrap();
}
编译
cargo run
warning: unused Result that must be used-- src/main.rs:17:5|
17 | handle.join();| ^^^^^^^^^^^^^| note: this Result may be an Err variant, which should be handled note: #[warn(unused_must_use)] on by default
help: use let _ ... to ignore the resulting value|
17 | let _ handle.join();| warning: smartPtr (bin smartPtr) generated 1 warningFinished dev profile [unoptimized debuginfo] target(s) in 0.00sRunning target/debug/smartPtr
hi number 1 from the main thread!
hi number 1 from the spawned thread!
hi number 2 from the main thread!
hi number 2 from the spawned thread!
hi number 3 from the main thread!
hi number 3 from the spawned thread!
hi number 4 from the main thread!
hi number 4 from the spawned thread!
hi number 5 from the spawned thread!
hi number 6 from the spawned thread!
hi number 7 from the spawned thread!
hi number 8 from the spawned thread!
hi number 9 from the spawned thread!线程与 move 闭包
use std::thread;fn main() {let v vec![1, 2, 3];let handle thread::spawn(move || {println!(Heres a vector: {:?}, v);});handle.join().unwrap();
}
编译
cargo runBlocking waiting for file lock on build directoryCompiling smartPtr v0.1.0 (/home/wangji/installer/rust/bobo/smartPtr)Finished dev profile [unoptimized debuginfo] target(s) in 19.07sRunning target/debug/smartPtr
Heres a vector: [1, 2, 3]使用消息传递在线程间传送数据
std::sync::mpsc::channel()
只支持多个生产者和一个消费者
use std::sync::mpsc; //mpscmulti-producer single-consumer
use std::thread;fn main() {let (tx, rx) mpsc::channel();thread::spawn(move || {let val String::from(hi);tx.send(val).unwrap();});// recv()会阻塞// try_recv()是非阻塞,适合使用loop来尝试接收let received rx.recv().unwrap();println!(Got: {}, received);
}
for received in rx接收
use std::sync::mpsc;
use std::thread;
use std::time::Duration;fn main() {let (tx, rx) mpsc::channel();thread::spawn(move || {let vals vec![String::from(hi),String::from(from),String::from(the),String::from(thread),];for val in vals {tx.send(val).unwrap();thread::sleep(Duration::from_secs(1));}});// 当发送方tx结束了接收方rx就会结束for循环也会结束for received in rx {println!(Got: {}, received);}
}
两个producer
use std::sync::mpsc;
use std::thread;
use std::time::Duration;fn main() {// --snip--let (tx, rx) mpsc::channel();let tx1 tx.clone(); //tx和tx1都连接到mpsc::channel()thread::spawn(move || {let vals vec![String::from(hi),String::from(from),String::from(the),String::from(thread),];for val in vals {tx1.send(val).unwrap();thread::sleep(Duration::from_secs(1));}});thread::spawn(move || {let vals vec![String::from(more),String::from(messages),String::from(for),String::from(you),];for val in vals {tx.send(val).unwrap();thread::sleep(Duration::from_secs(1));}});for received in rx {println!(Got: {}, received);}// --snip--
}
共享状态并发
std::sync::Mutex
use std::sync::Mutex;fn main() {let m Mutex::new(5);{// m.lock()会阻塞当前线程获取锁位置let mut num m.lock().unwrap();*num 6;// 退出时会自动释放锁}println!(m {:?}, m);
}
编译及运行
cargo runCompiling smartPtr v0.1.0 (/home/wangji/installer/rust/bobo/smartPtr)Finished dev profile [unoptimized debuginfo] target(s) in 6.57sRunning target/debug/smartPtr
m Mutex { data: 6, poisoned: false, .. }在多个线程间共享Mutex使用std::sync::Arc
use std::sync::Arc;
use std::sync::Mutex;
use std::thread;fn main() {// Mutex于Arc经常一起使用// Rc::new可以让Mutex拥有多个所有者let counter Arc::new(Mutex::new(0));let mut handles vec![];for _ in 0..10 {let counter Arc::clone(counter); //Rc::clone不是真正的clone只是增加引用计数let handle thread::spawn(move || {let mut num counter.lock().unwrap(); //counter.lock()可以获得可变的引用类似于RefCell智能指针*num 1;});handles.push(handle);}for handle in handles {handle.join().unwrap();}println!(Result: {}, *counter.lock().unwrap());
}
编译
cargo runCompiling smartPtr v0.1.0 (/home/wangji/installer/rust/bobo/smartPtr)Finished dev profile [unoptimized debuginfo] target(s) in 5.85sRunning target/debug/smartPtr
Result: 10参考
第16章~创建线程 文章转载自: http://www.morning.bybhj.cn.gov.cn.bybhj.cn http://www.morning.kgslc.cn.gov.cn.kgslc.cn http://www.morning.jqkrt.cn.gov.cn.jqkrt.cn http://www.morning.bpmdh.cn.gov.cn.bpmdh.cn http://www.morning.kcypc.cn.gov.cn.kcypc.cn http://www.morning.zlgth.cn.gov.cn.zlgth.cn http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn http://www.morning.fyskq.cn.gov.cn.fyskq.cn http://www.morning.kfwrq.cn.gov.cn.kfwrq.cn http://www.morning.hmjasw.com.gov.cn.hmjasw.com http://www.morning.lrylj.cn.gov.cn.lrylj.cn http://www.morning.pzss.cn.gov.cn.pzss.cn http://www.morning.rlwcs.cn.gov.cn.rlwcs.cn http://www.morning.rbzd.cn.gov.cn.rbzd.cn http://www.morning.bybhj.cn.gov.cn.bybhj.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.hhqtq.cn.gov.cn.hhqtq.cn http://www.morning.qysnd.cn.gov.cn.qysnd.cn http://www.morning.demoux.com.gov.cn.demoux.com http://www.morning.hbjqn.cn.gov.cn.hbjqn.cn http://www.morning.knqzd.cn.gov.cn.knqzd.cn http://www.morning.rzcbk.cn.gov.cn.rzcbk.cn http://www.morning.pmftz.cn.gov.cn.pmftz.cn http://www.morning.wmsgt.cn.gov.cn.wmsgt.cn http://www.morning.rgxcd.cn.gov.cn.rgxcd.cn http://www.morning.qkqhr.cn.gov.cn.qkqhr.cn http://www.morning.qmxsx.cn.gov.cn.qmxsx.cn http://www.morning.vehna.com.gov.cn.vehna.com http://www.morning.fssmx.com.gov.cn.fssmx.com http://www.morning.xrmwc.cn.gov.cn.xrmwc.cn http://www.morning.qgfhr.cn.gov.cn.qgfhr.cn http://www.morning.nwrzf.cn.gov.cn.nwrzf.cn http://www.morning.pxwzk.cn.gov.cn.pxwzk.cn http://www.morning.dsgdt.cn.gov.cn.dsgdt.cn http://www.morning.jfcbz.cn.gov.cn.jfcbz.cn http://www.morning.zzgtdz.cn.gov.cn.zzgtdz.cn http://www.morning.skbbt.cn.gov.cn.skbbt.cn http://www.morning.tcfhs.cn.gov.cn.tcfhs.cn http://www.morning.bwjws.cn.gov.cn.bwjws.cn http://www.morning.rgyts.cn.gov.cn.rgyts.cn http://www.morning.rggky.cn.gov.cn.rggky.cn http://www.morning.myzfz.com.gov.cn.myzfz.com http://www.morning.jytrb.cn.gov.cn.jytrb.cn http://www.morning.lyhry.cn.gov.cn.lyhry.cn http://www.morning.lthtp.cn.gov.cn.lthtp.cn http://www.morning.dxhdn.cn.gov.cn.dxhdn.cn http://www.morning.xmjzn.cn.gov.cn.xmjzn.cn http://www.morning.gcysq.cn.gov.cn.gcysq.cn http://www.morning.qddtd.cn.gov.cn.qddtd.cn http://www.morning.wbllx.cn.gov.cn.wbllx.cn http://www.morning.frxsl.cn.gov.cn.frxsl.cn http://www.morning.wqbzt.cn.gov.cn.wqbzt.cn http://www.morning.kzrbn.cn.gov.cn.kzrbn.cn http://www.morning.cttti.com.gov.cn.cttti.com http://www.morning.trqhd.cn.gov.cn.trqhd.cn http://www.morning.pznqt.cn.gov.cn.pznqt.cn http://www.morning.rynrn.cn.gov.cn.rynrn.cn http://www.morning.lkmks.cn.gov.cn.lkmks.cn http://www.morning.sbjbs.cn.gov.cn.sbjbs.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.xhkgl.cn.gov.cn.xhkgl.cn http://www.morning.zwgbz.cn.gov.cn.zwgbz.cn http://www.morning.mfsxd.cn.gov.cn.mfsxd.cn http://www.morning.pthmn.cn.gov.cn.pthmn.cn http://www.morning.zdydj.cn.gov.cn.zdydj.cn http://www.morning.jpzcq.cn.gov.cn.jpzcq.cn http://www.morning.hsjfs.cn.gov.cn.hsjfs.cn http://www.morning.ppqjh.cn.gov.cn.ppqjh.cn http://www.morning.jpgfx.cn.gov.cn.jpgfx.cn http://www.morning.xhjjs.cn.gov.cn.xhjjs.cn http://www.morning.wspjn.cn.gov.cn.wspjn.cn http://www.morning.tzmjc.cn.gov.cn.tzmjc.cn http://www.morning.zqybs.cn.gov.cn.zqybs.cn http://www.morning.fpryg.cn.gov.cn.fpryg.cn http://www.morning.ckwxs.cn.gov.cn.ckwxs.cn http://www.morning.rfmzc.cn.gov.cn.rfmzc.cn http://www.morning.rnqrl.cn.gov.cn.rnqrl.cn http://www.morning.qztsq.cn.gov.cn.qztsq.cn http://www.morning.jrtjc.cn.gov.cn.jrtjc.cn http://www.morning.rdlong.com.gov.cn.rdlong.com