|
| 1 | +// Mutex that protects the data vector, and then we spawn three threads |
| 2 | +//that each acquire a lock on the mutex and modify an element of the vector. |
| 3 | +// https://doc.rust-lang.org/std/sync/struct.Mutex.html |
| 4 | + |
| 5 | +use std::sync::{Arc, Condvar, Mutex, RwLock}; |
| 6 | +use std::thread; |
| 7 | + |
| 8 | +fn using_mutex() { |
| 9 | + println!("Mutex Example"); |
| 10 | + let data = Arc::new(Mutex::new(vec![1, 2, 3])); |
| 11 | + |
| 12 | + let handles: Vec<_> = (0..3) |
| 13 | + .map(|i| { |
| 14 | + let data = Arc::clone(&data); |
| 15 | + thread::spawn(move || { |
| 16 | + let mut data = data.lock().unwrap(); |
| 17 | + data[i] += 1; |
| 18 | + println!("{:?}", data); |
| 19 | + }) |
| 20 | + }) |
| 21 | + .collect(); |
| 22 | + |
| 23 | + for handle in handles { |
| 24 | + handle.join().unwrap(); |
| 25 | + } |
| 26 | + |
| 27 | + let data = data.lock().unwrap(); |
| 28 | + println!("Final data: {:?}", *data); |
| 29 | +} |
| 30 | + |
| 31 | +fn using_rwlock() { |
| 32 | + println!("RwLock Example"); |
| 33 | + // Create an RwLock protecting a vector |
| 34 | + let data = Arc::new(RwLock::new(vec![1, 2, 3])); |
| 35 | + |
| 36 | + // Spawn three threads that each acquire a write lock on the RwLock and modify an element of the vector |
| 37 | + let handles: Vec<_> = (0..3) |
| 38 | + .map(|i| { |
| 39 | + let data = Arc::clone(&data); |
| 40 | + thread::spawn(move || { |
| 41 | + let mut data = data.write().unwrap(); |
| 42 | + data[i] += 1; |
| 43 | + println!("Thread {}: {:?}", i, *data); |
| 44 | + }) |
| 45 | + }) |
| 46 | + .collect(); |
| 47 | + |
| 48 | + // Wait for all threads to finish |
| 49 | + for handle in handles { |
| 50 | + handle.join().unwrap(); |
| 51 | + } |
| 52 | + |
| 53 | + // Acquire a read lock to print the final state of the vector |
| 54 | + let data = data.read().unwrap(); |
| 55 | + |
| 56 | + println!("Final data: {:?}", *data); |
| 57 | +} |
| 58 | + |
| 59 | +fn using_condvar() { |
| 60 | + println!("Condvar Example"); |
| 61 | + let data = Arc::new((Mutex::new(vec![1, 2, 3]), Condvar::new())); |
| 62 | + let mut handles = vec![]; |
| 63 | + |
| 64 | + for i in 0..3 { |
| 65 | + let data = Arc::clone(&data); |
| 66 | + let handle = thread::spawn(move || { |
| 67 | + let (lock, cvar) = &*data; |
| 68 | + let mut data = lock.lock().unwrap(); |
| 69 | + data[i] += 1; |
| 70 | + println!("Thread {}: {:?}", i, *data); |
| 71 | + cvar.notify_one(); |
| 72 | + }); |
| 73 | + handles.push(handle); |
| 74 | + } |
| 75 | + |
| 76 | + for handle in handles { |
| 77 | + handle.join().unwrap(); |
| 78 | + } |
| 79 | + |
| 80 | + let (lock, cvar) = &*data; |
| 81 | + let mut data = lock.lock().unwrap(); |
| 82 | + while data.iter().any(|&x| x == 1) { |
| 83 | + data = cvar.wait(data).unwrap(); |
| 84 | + } |
| 85 | + |
| 86 | + println!("Final data: {:?}", *data); |
| 87 | +} |
| 88 | + |
| 89 | +fn main() { |
| 90 | + using_mutex(); |
| 91 | + using_rwlock(); |
| 92 | + using_condvar(); |
| 93 | +} |
| 94 | + |
| 95 | +/* |
| 96 | +
|
| 97 | +// This code will not compile because the borrow checker will prevent it. |
| 98 | +use std::thread; |
| 99 | +
|
| 100 | +fn main() { |
| 101 | + let mut data = vec![1, 2, 3]; |
| 102 | +
|
| 103 | + for i in 0..3 { |
| 104 | + // Try to capture a mutable reference in multiple threads |
| 105 | + // This will fail to compile! |
| 106 | + thread::spawn(move || { |
| 107 | + data[i] += 1; |
| 108 | + }); |
| 109 | + } |
| 110 | +
|
| 111 | + // No data race can occur, this will not compile. |
| 112 | +} |
| 113 | +*/ |
0 commit comments