File tree Expand file tree Collapse file tree 3 files changed +52
-0
lines changed
Expand file tree Collapse file tree 3 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -153,3 +153,9 @@ pub usingnamespace @cImport({
153153
154154> [ !IMPORTANT]
155155> 初次阅读此处困惑是正常的,后面的概念学习完成后此处自通。
156+
157+ ## ` threadlocal `
158+
159+ 变量可以使用 ` threadlocal ` 修饰,来使得该变量在不同线程中是不同的示例:
160+
161+ <<<@/code/release/define_variable.zig#threadlocal
Original file line number Diff line number Diff line change @@ -146,3 +146,26 @@ const Deconstruct = struct {
146146 _ = x ;
147147 }
148148};
149+
150+ const ThreadLocal = struct {
151+ // #region threadlocal
152+ const std = @import ("std" );
153+ threadlocal var x : i32 = 1234 ;
154+
155+ fn main () ! void {
156+ const thread1 = try std .Thread .spawn (.{}, testTls , .{});
157+ const thread2 = try std .Thread .spawn (.{}, testTls , .{});
158+ testTls ();
159+ thread1 .join ();
160+ thread2 .join ();
161+ }
162+
163+ fn testTls () void {
164+ // 1234
165+ std .debug .print ("x is {}\n " , .{x });
166+ x += 1 ;
167+ // 1235
168+ std .debug .print ("x is {}\n " , .{x });
169+ }
170+ // #endregion threadlocal
171+ };
Original file line number Diff line number Diff line change @@ -146,3 +146,26 @@ const Deconstruct = struct {
146146 _ = x ;
147147 }
148148};
149+
150+ const ThreadLocal = struct {
151+ // #region threadlocal
152+ const std = @import ("std" );
153+ threadlocal var x : i32 = 1234 ;
154+
155+ fn main () ! void {
156+ const thread1 = try std .Thread .spawn (.{}, testTls , .{});
157+ const thread2 = try std .Thread .spawn (.{}, testTls , .{});
158+ testTls ();
159+ thread1 .join ();
160+ thread2 .join ();
161+ }
162+
163+ fn testTls () void {
164+ // 1234
165+ std .debug .print ("x is {}\n " , .{x });
166+ x += 1 ;
167+ // 1235
168+ std .debug .print ("x is {}\n " , .{x });
169+ }
170+ // #endregion threadlocal
171+ };
You can’t perform that action at this time.
0 commit comments