-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1a66ec
commit cc8f8cc
Showing
5 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Check file existence | ||
|
||
In this example, we use `panic` to ensure `build.zig` should exist. | ||
```zig | ||
{{#include ../src/01-04.zig}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
## Thread pool | ||
|
||
Thread pools address two different problems: | ||
1. They usually provide improved performance when executing large numbers of asynchronous tasks, due to reduced per-task invocation overhead, and | ||
2. They provide a means of bounding and managing the resources, including threads, consumed when executing a collection of tasks. | ||
|
||
|
||
In this example, we spawn 10 tasks into thread pool, and use `WaitGroup` to wait for them to finish. | ||
|
||
```zig | ||
{{#include ../src/07-03.zig}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//! Check file existence | ||
|
||
const std = @import("std"); | ||
const fs = std.fs; | ||
|
||
pub fn main() void { | ||
const filename = "build.zig"; | ||
fs.cwd().access(filename, .{}) catch { | ||
std.debug.panic("{s} not exists", .{filename}); | ||
}; | ||
|
||
std.debug.print("{s} exists", .{filename}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const std = @import("std"); | ||
const print = std.debug.print; | ||
|
||
pub fn main() !void { | ||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||
defer arena.deinit(); | ||
const allocator = arena.allocator(); | ||
|
||
var pool: std.Thread.Pool = undefined; | ||
try pool.init(.{ | ||
.allocator = allocator, | ||
.n_jobs = 4, | ||
}); | ||
defer pool.deinit(); | ||
|
||
var wg: std.Thread.WaitGroup = .{}; | ||
for (0..10) |i| { | ||
pool.spawnWg(&wg, struct { | ||
fn run(id: usize) void { | ||
print("I'm from {d}\n", .{id}); | ||
} | ||
}.run, .{i}); | ||
} | ||
wg.wait(); | ||
|
||
print("All threads exit.\n", .{}); | ||
} |