thread.zig (1012B)
1 const std = @import("std"); 2 const ProgressBar = @import("progress").Bar; 3 4 pub fn threadWorker(bar: *ProgressBar, seed: usize) !void { 5 var rand_impl = std.Random.DefaultPrng.init(seed); 6 const num = @mod(rand_impl.random().int(i64), 1000); 7 8 for (0..5) |_| { 9 bar.add(1); 10 try bar.render(); 11 12 try std.Io.sleep(bar.writer.io, std.Io.Duration.fromMilliseconds(num), .awake); 13 } 14 } 15 16 pub fn main(init: std.process.Init) !void { 17 var stdout_buf: [4096]u8 = undefined; 18 var stdout_writer = std.Io.File.stdout().writer(init.io, &stdout_buf); 19 var pb = ProgressBar.init(100, &stdout_writer, .{ 20 .description = "Progress bar with threads", 21 .show_iterations = true, 22 .show_percentage = true, 23 }); 24 25 const thread_count = 20; 26 var threads: [thread_count]std.Thread = undefined; 27 for (0..thread_count) |i| { 28 threads[i] = try std.Thread.spawn(.{}, threadWorker, .{ &pb, i }); 29 } 30 31 for (threads) |thread| { 32 thread.join(); 33 } 34 }