summaryrefslogtreecommitdiff
path: root/exercises/090_async7.zig
blob: f914aef7f587a533eff5f05407ee567d40fd0527 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// When multiple async tasks access shared data, you need
// synchronization! Io provides a Mutex for this:
//
//     var mutex: std.Io.Mutex = .init;
//
//     // In a task:
//     try mutex.lock(io);       // blocks until lock is acquired
//     defer mutex.unlock();
//     // ... critical section: safe to modify shared data ...
//
// Without the mutex, concurrent tasks could read and write the
// same memory simultaneously, causing a data race — the result
// would be unpredictable.
//
// mutex.lock() is a cancellation point — it can return
// error.Canceled. There's also tryLock() which returns
// immediately (true if acquired, false if not).
//
// Fix this program so the counter is correctly synchronized.
// Without the fix, the final count would be unpredictable.
// With it, four tasks incrementing 100 times each = 400.
//
const std = @import("std");
const print = std.debug.print;

const SharedState = struct {
    counter: u32 = 0,
    mutex: std.Io.Mutex = .init,
};

pub fn main(init: std.process.Init) !void {
    const io = init.io;
    var state = SharedState{};

    var group: std.Io.Group = .init;

    group.async(io, increment, .{ io, &state, 100 });
    group.async(io, increment, .{ io, &state, 100 });
    group.async(io, increment, .{ io, &state, 100 });
    group.async(io, increment, .{ io, &state, 100 });

    try group.await(io);

    print("Counter: {} (expected: 400)\n", .{state.counter});
}

fn increment(io: std.Io, state: *SharedState, times: u32) void {
    for (0..times) |_| {
        // Acquire the lock before modifying shared state.
        // What Mutex method blocks until the lock is acquired?
        state.mutex.??? catch return;
        defer state.mutex.unlock(); // <-- what's missing here?

        state.counter += 1;
    }
}