| 1 | /* |
| 2 | This code demonstrates thread safety using atomic operations in V. |
| 3 | |
| 4 | Thread safety is achieved by using atomic functions to manipulate the shared counter variable. |
| 5 | Atomic operations ensure that the read-modify-write sequence is performed as a single, indivisible operation, |
| 6 | preventing race conditions and ensuring data integrity when accessed by multiple threads concurrently. |
| 7 | |
| 8 | Key points: |
| 9 | 1. **Atomic Fetch and Add**: The `C.atomic_fetch_add_u32` function atomically increments the counter. |
| 10 | This means that the increment operation is performed without interruption, ensuring that no two threads |
| 11 | can increment the counter simultaneously and cause a race condition. |
| 12 | |
| 13 | 2. **Atomic Load**: The `C.atomic_load_u32` function atomically reads the value of the counter. |
| 14 | This ensures that the read operation is consistent and not affected by concurrent writes from other threads. |
| 15 | |
| 16 | 3. **Thread Synchronization**: The `spawn` function is used to create new threads that run the `increment` function. |
| 17 | The `wait` method is called on each thread to ensure that the main thread waits for both threads to complete |
| 18 | before reading the final value of the counter. |
| 19 | |
| 20 | By using atomic operations and proper thread synchronization, the code ensures that the shared counter is |
| 21 | incremented safely and correctly by multiple threads. |
| 22 | */ |
| 23 | import sync as _ |
| 24 | |
| 25 | // Function to increment the atomic counter |
| 26 | fn increment(atomic_counter &u32) { |
| 27 | C.atomic_fetch_add_u32(atomic_counter, 1) |
| 28 | } |
| 29 | |
| 30 | fn main() { |
| 31 | atomic_counter := u32(0) // Atomic counter variable |
| 32 | |
| 33 | // Spawn two threads that increment the atomic counter |
| 34 | t1 := spawn increment(&atomic_counter) |
| 35 | t2 := spawn increment(&atomic_counter) |
| 36 | |
| 37 | // Wait for both threads to complete |
| 38 | t1.wait() |
| 39 | t2.wait() |
| 40 | |
| 41 | // Load and print the final value of the atomic counter |
| 42 | final_count := C.atomic_load_u32(&atomic_counter) |
| 43 | println('Atomic Counter: ${final_count}') |
| 44 | } |
| 45 | |