Greater gradation of timers built into the threads

This commit is contained in:
Chris Boesch 2024-04-10 16:06:23 +02:00
parent c0ff1b70c1
commit 7732cd7716
No known key found for this signature in database
GPG key ID: 8712DF4D3E364668
2 changed files with 7 additions and 3 deletions

View file

@ -118,7 +118,11 @@ pub fn main() !void {
// In our example, we pass the number of the thread as a parameter. // In our example, we pass the number of the thread as a parameter.
fn thread_function(num: usize) !void { fn thread_function(num: usize) !void {
std.debug.print("thread {d}: {s}\n", .{ num, "started." }); std.debug.print("thread {d}: {s}\n", .{ num, "started." });
std.time.sleep((5 - num % 3) * std.time.ns_per_s);
// This timer simulates the work of the thread.
const work_time = 2 * ((5 - num % 3) - 2);
std.time.sleep(work_time * std.time.ns_per_s);
std.debug.print("thread {d}: {s}\n", .{ num, "finished." }); std.debug.print("thread {d}: {s}\n", .{ num, "finished." });
} }
// This is the easiest way to run threads in parallel. // This is the easiest way to run threads in parallel.

View file

@ -1,5 +1,5 @@
--- exercises/104_threading.zig 2024-03-05 09:09:04.013974229 +0100 --- exercises/104_threading.zig 2024-04-10 16:02:33.680771772 +0200
+++ answers/104_threading.zig 2024-03-05 09:12:03.987162883 +0100 +++ answers/104_threading.zig 2024-04-10 16:01:00.042331517 +0200
@@ -97,12 +97,12 @@ @@ -97,12 +97,12 @@
defer handle.join(); defer handle.join();