Совместный доступ с Arc и Mutex
Безопасно изменяйте общее состояние.
«Совместный доступ с Arc и Mutex» — бесплатный урок Learn Rust Coding на CoddyKit. Это урок 3 из 4. Ты можешь прочитать весь урок бесплатно ниже — а потом практиковать его прямо в браузере с встроенным редактором кода и ИИ-репетитором 24/7. Это часть пути обучения Learn Rust Coding, и твой прогресс синхронизируется между веб-версией и приложением CoddyKit. Курс Learn Rust Coding содержит 4 уроков всего.
Части этого урока еще не переведены и отображаются на английском.
The Need for Shared State
Sometimes several threads must read or update the same data. Moving ownership to a single thread is not enough.
Rust gives you safe shared ownership across threads with Arc, and safe mutation with Mutex. Together they enable shared, mutable state.
Arc: Atomic Reference Counting
Arc stands for atomically reference counted. It is like Rc, but its counter uses atomic operations, so it is safe across threads.
Cloning an Arc does not copy the data. It only increments the count and returns another handle to the same value.
use std::sync::Arc;
fn main() {
let shared = Arc::new(vec![1, 2, 3]);
let clone = Arc::clone(&shared);
println!("{:?} {:?}", shared, clone);
}Sharing Arc Across Threads
To share read-only data, clone the Arc once per thread and move each clone in. All threads point to the same allocation.
Because Arc is Send and Sync, this compiles cleanly.
use std::sync::Arc;
use std::thread;
fn main() {
let data = Arc::new(vec![10, 20, 30]);
let mut handles = vec![];
for i in 0..3 {
let d = Arc::clone(&data);
handles.push(thread::spawn(move || println!("{}", d[i])));
}
for h in handles { h.join().unwrap(); }
}Arc Alone Is Read-Only
Arc gives you shared ownership, but only shared, immutable access to the inner value.
You cannot mutate data through an Arc directly, because multiple threads holding it at once would race. You need interior mutability with a lock.
Mutex: Mutual Exclusion
A Mutex guards data so only one thread can access it at a time. You call lock to get access.
lock returns a Result; unwrapping gives a smart pointer guard. Other threads block until the guard is dropped.
use std::sync::Mutex;
fn main() {
let m = Mutex::new(0);
{
let mut guard = m.lock().unwrap();
*guard += 5;
}
println!("{:?}", m);
}The Guard and RAII
The value returned by lock is a MutexGuard. You access the inner data by dereferencing it with *.
When the guard goes out of scope, the lock is released automatically. This RAII style prevents forgetting to unlock.
Combining Arc and Mutex
To share mutable state across threads, wrap a Mutex inside an Arc. The Arc shares ownership; the Mutex guards mutation.
This pattern, Arc<Mutex<T>>, is the standard way to do shared mutable state in Rust.
use std::sync::{Arc, Mutex};
fn main() {
let counter = Arc::new(Mutex::new(0));
let c = Arc::clone(&counter);
*c.lock().unwrap() += 1;
println!("{}", *counter.lock().unwrap());
}A Shared Counter
Here ten threads each increment a shared counter. Each clones the Arc, locks the Mutex, and adds one.
Because the lock serializes access, the final total is always exactly ten.
use std::sync::{Arc, Mutex};
use std::thread;
fn main() {
let counter = Arc::new(Mutex::new(0));
let mut handles = vec![];
for _ in 0..10 {
let c = Arc::clone(&counter);
handles.push(thread::spawn(move || {
*c.lock().unwrap() += 1;
}));
}
for h in handles { h.join().unwrap(); }
println!("{}", *counter.lock().unwrap());
}Keep Critical Sections Short
The code while a lock is held is the critical section. Other threads wait there, so keep it short.
Lock, do the minimal update, then release. Avoid heavy computation or I/O while holding the lock.
use std::sync::Mutex;
fn main() {
let m = Mutex::new(Vec::new());
{
let mut v = m.lock().unwrap();
v.push(1);
} // lock released here
println!("{:?}", m.lock().unwrap());
}Deadlocks and Poisoning
If a thread locks the same Mutex twice, or two threads lock two mutexes in opposite orders, you can deadlock and hang forever.
If a thread panics while holding a lock, the Mutex becomes poisoned, and later lock calls return an Err.
RwLock for Many Readers
When reads vastly outnumber writes, RwLock can be better than Mutex. It allows many concurrent readers or one exclusive writer.
Use read for shared access and write for exclusive access.
use std::sync::RwLock;
fn main() {
let lock = RwLock::new(5);
{
let r = lock.read().unwrap();
println!("read {}", *r);
}
*lock.write().unwrap() += 1;
println!("{}", *lock.read().unwrap());
}Quick Check
Test your understanding of Arc and Mutex.
Recap
You learned that Arc shares ownership across threads with atomic reference counting, but only allows immutable access.
A Mutex guards mutation, handing out a guard that releases on drop. Combine them as Arc<Mutex<T>> for shared mutable state, watching for deadlocks and poisoning.
Next you will join threads and collect their results.
Часто задаваемые вопросы
Урок «Совместный доступ с Arc и Mutex» бесплатный?
Да — полный текст урока «Совместный доступ с Arc и Mutex» бесплатно доступен здесь в веб-версии. Чтобы практиковать его интерактивно (встроенный редактор кода и ИИ-репетитор 24/7) и разблокировать остальной курс Learn Rust Coding, подпишись на CoddyKit PRO. Курс Learn Rust Coding содержит 4 уроков всего.
Чему я научусь в уроке «Совместный доступ с Arc и Mutex»?
Безопасно изменяйте общее состояние. Ты практикуешь Learn Rust Coding с помощью реального кода, который запускаешь прямо в браузере, и ИИ-репетитор 24/7 отвечает на твои вопросы во время урока.
Нужен ли мне опыт, чтобы начать Learn Rust Coding?
Предыдущий опыт не требуется. Learn Rust Coding на CoddyKit структурирован для всех уровней — от новичков до продвинутых, поэтому ты можешь начать отсюда или с самого начала и учиться в своем темпе. Это урок 3 из 4.
Сколько времени занимает урок «Совместный доступ с Arc и Mutex»?
Большинство уроков CoddyKit занимают около 5–10 минут. Каждый из них компактный и интерактивный, поэтому ты постоянно делаешь прогресс и продолжаешь с того же места в веб-версии и приложении.
Можно ли писать и запускать код в этом уроке Learn Rust Coding?
Да. Каждый урок Learn Rust Coding включает встроенный редактор кода, поэтому ты пишешь и запускаешь реальный код прямо в браузере и получаешь моментальную обратную связь от AI — локальная установка не требуется.
Все уроки этого курса
- Запуск потоков
- Передача данных в потоки
- Совместный доступ с Arc и Mutex
- Объединение потоков и сбор результатов