-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasymmetric.zig
65 lines (54 loc) · 2.07 KB
/
asymmetric.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! One producer thread, one consumer thread.
const builtin = @import("builtin");
const std = @import("std");
const Allocator = std.mem.Allocator;
const ThreadSafeQueue = @import("ThreadSafeQueue.zig").ThreadSafeQueue;
var debug_allocator: std.heap.DebugAllocator(.{
.safety = false,
.stack_trace_frames = 0,
}) = .init;
pub fn main() !void {
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const arena = arena_instance.allocator();
const args = try std.process.argsAlloc(arena);
const Which = enum { debug, smp, c };
const which = std.meta.stringToEnum(Which, args[1]) orelse @panic("bad allocator choice");
const gpa = switch (which) {
.debug => debug_allocator.allocator(),
.smp => std.heap.smp_allocator,
.c => if (builtin.link_libc) std.heap.raw_c_allocator else @panic("need link libc"),
};
queue.state = .run;
const producer = try std.Thread.spawn(.{}, producerRun, .{gpa});
defer producer.join();
const consumer = try std.Thread.spawn(.{}, consumerRun, .{gpa});
defer consumer.join();
}
var queue: ThreadSafeQueue([]const u8) = .empty;
var event: std.Thread.ResetEvent = .{};
fn producerRun(gpa: Allocator) void {
producerRunFallible(gpa) catch @panic("OOM");
}
fn producerRunFallible(gpa: Allocator) !void {
for (0..10000000) |n| {
const string = try std.fmt.allocPrint(gpa, "{d}*{d} = {d}", .{ n, n, n * n });
if (try queue.enqueue(gpa, &.{string})) event.set();
}
if (try queue.enqueue(gpa, &.{"end"})) event.set();
}
fn consumerRun(gpa: Allocator) void {
var sum: usize = 0;
outer: while (true) {
event.reset();
while (queue.check()) |strings| {
for (strings) |string| {
if (std.mem.eql(u8, string, "end")) break :outer;
defer gpa.free(string);
for (string) |byte| sum += byte;
}
}
event.wait();
}
//std.io.getStdOut().writer().print("sum: {d}\n", .{sum}) catch @panic("write failure");
if (sum != 16002606398) @panic("wrong answer");
}