This commit is contained in:
TimLai666 2024-06-18 18:52:27 +08:00 committed by GitHub
parent 8f6d8ec21b
commit 28b05c6d26
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 27 deletions

View File

@ -1,10 +1,9 @@
# Threads
# 執行緒(Threads
In most current operating systems, an executed program's code is run in a process, and the operating system manages multiple processes at once.
Within your program, you can also have independent parts that run simultaneously. The features that run these independent parts are called threads.
在大多數現代作業系統中,執行程式的代碼在一個進程中運行,作業系統同時管理多個進程。在您的程式中,您還可以有同時運行的獨立部分。這些獨立運行部分的功能稱為執行緒。
## Further information
## 進一步了解
- [Dining Philosophers example](https://doc.rust-lang.org/1.4.0/book/dining-philosophers.html)
- [Using Threads to Run Code Simultaneously](https://doc.rust-lang.org/book/ch16-01-threads.html)
- [Using Message Passing to Transfer Data Between Threads](https://doc.rust-lang.org/book/ch16-02-message-passing.html)
- [哲學家就餐問題示例](https://doc.rust-lang.org/1.4.0/book/dining-philosophers.html)
- [使用執行緒同時運行代碼](https://doc.rust-lang.org/book/ch16-01-threads.html)
- [使用訊息傳遞在執行緒之間傳輸資料](https://doc.rust-lang.org/book/ch16-02-message-passing.html)

View File

@ -1,12 +1,9 @@
// threads1.rs
//
// This program spawns multiple threads that each run for at least 250ms, and
// each thread returns how much time they took to complete. The program should
// wait until all the spawned threads have finished and should collect their
// return values into a vector.
// 這個程式會生成多個執行緒每個執行緒至少執行250毫秒並回傳它們完成所需的時間。
// 程式應該等待所有生成的執行緒完成,並將它們的回傳值收集到一個向量中。
//
// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a
// hint.
// 執行 `rustlings hint threads1` 或使用 `hint` watch 子命令以獲取提示。
// I AM NOT DONE
@ -26,15 +23,15 @@ fn main() {
let mut results: Vec<u128> = vec![];
for handle in handles {
// TODO: a struct is returned from thread::spawn, can you use it?
// TODO: 有一個結構體是從 thread::spawn 回傳的,你能使用它嗎?
}
if results.len() != 10 {
panic!("Oh no! All the spawned threads did not finish!");
panic!("哦不!所有生成的執行緒都沒有完成!");
}
println!();
for (i, result) in results.into_iter().enumerate() {
println!("thread {} took {}ms", i, result);
println!("thread {} 花費了 {} 毫秒", i, result);
}
}

View File

@ -1,11 +1,8 @@
// threads2.rs
//
// Building on the last exercise, we want all of the threads to complete their
// work but this time the spawned threads need to be in charge of updating a
// shared value: JobStatus.jobs_completed
// 基於上一個練習我們希望所有執行緒完成他們的工作但這次生成的執行緒需要負責更新一個共享的值JobStatus.jobs_completed
//
// Execute `rustlings hint threads2` or use the `hint` watch subcommand for a
// hint.
// 執行 `rustlings hint threads2` 或使用 `hint` watch 子命令以獲取提示。
// I AM NOT DONE
@ -18,7 +15,7 @@ struct JobStatus {
}
fn main() {
// TODO: `Arc` isn't enough if you want a **mutable** shared state
// TODO: 如果你想要一個**可變的**共享狀態,僅僅使用 `Arc` 是不夠的
let status = Arc::new(JobStatus { jobs_completed: 0 });
let mut handles = vec![];
@ -26,17 +23,17 @@ fn main() {
let status_shared = Arc::clone(&status);
let handle = thread::spawn(move || {
thread::sleep(Duration::from_millis(250));
// TODO: You must take an action before you update a shared value
// TODO: 在更新共享值之前,你必須採取一個動作
status_shared.jobs_completed += 1;
});
handles.push(handle);
}
// Waiting for all jobs to complete
// 等待所有工作完成
for handle in handles {
handle.join().unwrap();
}
// TODO: Print the value of `JobStatus.jobs_completed`
// TODO: 印出 `JobStatus.jobs_completed` 的值
println!("Jobs completed: {}", ???);
}

View File

@ -1,7 +1,6 @@
// threads3.rs
//
// Execute `rustlings hint threads3` or use the `hint` watch subcommand for a
// hint.
// 執行 `rustlings hint threads3` 或使用 `hint` watch 子命令以獲取提示。
// I AM NOT DONE