-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo.zig
More file actions
30 lines (25 loc) · 798 Bytes
/
demo.zig
File metadata and controls
30 lines (25 loc) · 798 Bytes
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
const std = @import("std");
const Ring = @import("spsc_ring.zig").Ring;
pub fn main() !void {
var buf: [256]u64 = undefined;
var ring = Ring(u64).init(buf[0..]);
var producer_thread = try std.Thread.spawn(.{}, producer, .{&ring});
var consumer_thread = try std.Thread.spawn(.{}, consumer, .{&ring});
producer_thread.join();
consumer_thread.join();
}
fn producer(ring: *Ring(u64)) void {
_ = ring.enqueue(42);
}
fn consumer(ring: *Ring(u64)) !void {
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
while (true) {
if (ring.dequeue()) |answer| {
try stdout.print("answer = {}\n", .{answer});
try bw.flush();
return;
}
}
}