Additional timer in thread start added

This commit is contained in:
Chris Boesch 2024-04-10 19:13:46 +02:00
parent 7732cd7716
commit ba2a9622c4
No known key found for this signature in database
GPG key ID: 8712DF4D3E364668
2 changed files with 5 additions and 4 deletions

View file

@ -106,7 +106,7 @@ pub fn main() !void {
// After the threads have been started, // After the threads have been started,
// they run in parallel and we can still do some work in between. // they run in parallel and we can still do some work in between.
std.time.sleep((1) * std.time.ns_per_s); std.time.sleep(1500 * std.time.ns_per_ms);
std.debug.print("Some weird stuff, after starting the threads.\n", .{}); std.debug.print("Some weird stuff, after starting the threads.\n", .{});
} }
// After we have left the closed area, we wait until // After we have left the closed area, we wait until
@ -117,10 +117,11 @@ pub fn main() !void {
// This function is started with every thread that we set up. // This function is started with every thread that we set up.
// 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.time.sleep(200 * num * std.time.ns_per_ms);
std.debug.print("thread {d}: {s}\n", .{ num, "started." }); std.debug.print("thread {d}: {s}\n", .{ num, "started." });
// This timer simulates the work of the thread. // This timer simulates the work of the thread.
const work_time = 2 * ((5 - num % 3) - 2); const work_time = 3 * ((5 - num % 3) - 2);
std.time.sleep(work_time * std.time.ns_per_s); 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." });

View file

@ -1,5 +1,5 @@
--- exercises/104_threading.zig 2024-04-10 16:02:33.680771772 +0200 --- exercises/104_threading.zig 2024-04-10 19:12:29.878856370 +0200
+++ answers/104_threading.zig 2024-04-10 16:01:00.042331517 +0200 +++ answers/104_threading.zig 2024-04-10 19:11:22.304265713 +0200
@@ -97,12 +97,12 @@ @@ -97,12 +97,12 @@
defer handle.join(); defer handle.join();