-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello-zig.zig
32 lines (26 loc) · 899 Bytes
/
hello-zig.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const std = @import("std");
pub fn main() !void {
std.debug.print("Sleep sorting\n", .{});
const values = [_]usize{ 9, 40, 10, 1, 6, 45, 23, 50 };
for (values) |num| {
std.debug.print("{} ", .{num});
}
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const allocator = arena.allocator();
defer arena.deinit();
std.debug.print("\nSort numbers: ", .{});
try sleepSort([values.len]usize, values, allocator);
std.debug.print("\n", .{});
}
fn sleepSort(comptime T: type, nums: T, alloc: std.mem.Allocator) !void {
var threadpool: std.Thread.Pool = undefined;
try threadpool.init(.{ .allocator = alloc });
defer threadpool.deinit();
for (nums) |num| {
try threadpool.spawn(sleep, .{num});
}
}
fn sleep(num: usize) void {
std.time.sleep(num * std.time.ns_per_ms);
std.debug.print("{} ", .{num});
}