about summary refs log tree commit diff
path: root/lang/zig/tls.zig
diff options
context:
space:
mode:
Diffstat (limited to 'lang/zig/tls.zig')
-rw-r--r--lang/zig/tls.zig22
1 files changed, 22 insertions, 0 deletions
diff --git a/lang/zig/tls.zig b/lang/zig/tls.zig
new file mode 100644
index 0000000..b47b17c
--- /dev/null
+++ b/lang/zig/tls.zig
@@ -0,0 +1,22 @@
+const assert = std.debug.assert;
+const std = @import("std");
+const spawn = std.Thread.spawn;
+
+threadlocal var x: i32 = 1234;
+
+test "thread local storage" {
+    const thread1 = try spawn({}, testTls);
+    defer thread1.wait();
+
+    const thread2 = try spawn({}, testTls);
+    defer thread2.wait();
+
+    // Main thread
+    testTls({});
+}
+
+fn testTls(context: void) void {
+    assert(x == 1234);
+    x += 1;
+    assert(x == 1235);
+}