diff options
| author | Chris Boesch <chrboesch@noreply.codeberg.org> | 2025-11-28 14:31:30 +0100 |
|---|---|---|
| committer | Chris Boesch <chrboesch@noreply.codeberg.org> | 2025-11-28 14:31:30 +0100 |
| commit | ed9694e557bcbe95e5d00489d32aec556614a52d (patch) | |
| tree | 2cb0edbf332c36cadbfb604e80bc73c21d38a1d6 | |
| parent | 39e346303cc6245ed66adc529c71ed05ea12c991 (diff) | |
| parent | dc416b6c5a1bfd44bcc79682732e27d39267e826 (diff) | |
Merge pull request 'changed 'sleep' to async I/O' (#328) from i323 into main
Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/328
| -rw-r--r-- | exercises/104_threading.zig | 10 | ||||
| -rw-r--r-- | patches/patches/104_threading.patch | 4 |
2 files changed, 9 insertions, 5 deletions
diff --git a/exercises/104_threading.zig b/exercises/104_threading.zig index 7c5e0f7..2b0e6f7 100644 --- a/exercises/104_threading.zig +++ b/exercises/104_threading.zig @@ -106,7 +106,9 @@ pub fn main() !void { // After the threads have been started, // they run in parallel and we can still do some work in between. - std.posix.nanosleep(4, 0); + var io_instance: std.Io.Threaded = .init_single_threaded; + const io = io_instance.io(); + try io.sleep(std.Io.Duration.fromSeconds(4), .awake); std.debug.print("Some weird stuff, after starting the threads.\n", .{}); } // After we have left the closed area, we wait until @@ -117,12 +119,14 @@ pub fn main() !void { // This function is started with every thread that we set up. // In our example, we pass the number of the thread as a parameter. fn thread_function(num: usize) !void { - std.posix.nanosleep(1 * num, 0); + var io_instance: std.Io.Threaded = .init_single_threaded; + const io = io_instance.io(); + try io.sleep(std.Io.Duration.fromSeconds(1 * @as(isize, @intCast(num))), .awake); std.debug.print("thread {d}: {s}\n", .{ num, "started." }); // This timer simulates the work of the thread. const work_time = 3 * ((5 - num % 3) - 2); - std.posix.nanosleep(work_time, 0); + try io.sleep(std.Io.Duration.fromSeconds(@intCast(work_time)), .awake); std.debug.print("thread {d}: {s}\n", .{ num, "finished." }); } diff --git a/patches/patches/104_threading.patch b/patches/patches/104_threading.patch index 1009a3a..eda25fd 100644 --- a/patches/patches/104_threading.patch +++ b/patches/patches/104_threading.patch @@ -1,5 +1,5 @@ ---- exercises/104_threading.zig 2025-11-01 15:54:27.074988112 +0100 -+++ answers/104_threading.zig 2025-11-01 15:56:12.852195135 +0100 +--- exercises/104_threading.zig 2025-11-28 14:17:31.552529679 +0100 ++++ answers/104_threading.zig 2025-11-28 14:15:36.823931851 +0100 @@ -97,12 +97,12 @@ defer handle.join(); |
