-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.zig
37 lines (28 loc) · 862 Bytes
/
example.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
const std = @import("std");
const umutex = @import("umutex.zig");
pub fn main() !void {
var da = std.heap.DirectAllocator.init();
defer da.deinit();
var alloc = &da.allocator;
var mutex = try umutex.Umutex.init(alloc);
defer alloc.destroy(mutex);
var work = Workdata{
.mutex = mutex,
.data = 0,
};
var threads: [10]*std.os.Thread = undefined;
for (threads) |*thread| {
thread.* = try std.os.spawnThread(&work, worker);
}
std.os.time.sleep(3 * std.os.time.ns_per_s);
std.debug.warn("Using {} threads, the final value is: {}\n", threads.len, work.data);
}
const Workdata = struct {
mutex: *umutex.Umutex,
data: u32,
};
fn worker(work: *Workdata) void {
_ = work.mutex.lockDelay(10 * std.os.time.millisecond);
defer _ = work.mutex.unlock();
work.data += 1;
}