my ziglings
0

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request 'changed 'sleep' to async I/O' (#328) from i323 into main

Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/328

Chris Boesch (Nov 28, 2025, 2:31 PM +0100) ed9694e5 39e34630

+9 -5
+7 -3
exercises/104_threading.zig
··· 106 106 107 107 // After the threads have been started, 108 108 // they run in parallel and we can still do some work in between. 109 - std.posix.nanosleep(4, 0); 109 + var io_instance: std.Io.Threaded = .init_single_threaded; 110 + const io = io_instance.io(); 111 + try io.sleep(std.Io.Duration.fromSeconds(4), .awake); 110 112 std.debug.print("Some weird stuff, after starting the threads.\n", .{}); 111 113 } 112 114 // After we have left the closed area, we wait until ··· 117 119 // This function is started with every thread that we set up. 118 120 // In our example, we pass the number of the thread as a parameter. 119 121 fn thread_function(num: usize) !void { 120 - std.posix.nanosleep(1 * num, 0); 122 + var io_instance: std.Io.Threaded = .init_single_threaded; 123 + const io = io_instance.io(); 124 + try io.sleep(std.Io.Duration.fromSeconds(1 * @as(isize, @intCast(num))), .awake); 121 125 std.debug.print("thread {d}: {s}\n", .{ num, "started." }); 122 126 123 127 // This timer simulates the work of the thread. 124 128 const work_time = 3 * ((5 - num % 3) - 2); 125 - std.posix.nanosleep(work_time, 0); 129 + try io.sleep(std.Io.Duration.fromSeconds(@intCast(work_time)), .awake); 126 130 127 131 std.debug.print("thread {d}: {s}\n", .{ num, "finished." }); 128 132 }
+2 -2
patches/patches/104_threading.patch
··· 1 - --- exercises/104_threading.zig 2025-11-01 15:54:27.074988112 +0100 2 - +++ answers/104_threading.zig 2025-11-01 15:56:12.852195135 +0100 1 + --- exercises/104_threading.zig 2025-11-28 14:17:31.552529679 +0100 2 + +++ answers/104_threading.zig 2025-11-28 14:15:36.823931851 +0100 3 3 @@ -97,12 +97,12 @@ 4 4 defer handle.join(); 5 5