Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add thread pool #74

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ on:
- "**.zig"
- ".github/**"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
examples:
timeout-minutes: 10
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ permissions:
id-token: write

concurrency:
group: "pages"
cancel-in-progress: false
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true


jobs:
build:
runs-on: ubuntu-latest
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
steps:
- uses: actions/checkout@v4
with:
Expand Down
6 changes: 6 additions & 0 deletions book-src/01-04-file-exists.md
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}}
```
12 changes: 12 additions & 0 deletions book-src/07-03-threadpool.md
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}}
```
2 changes: 2 additions & 0 deletions book-src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [Read file line by line](./01-01-read-file-line-by-line.md)
- [Mmap file](./01-02-mmap-file.md)
- [Find Files modified in the last 24 hours](./01-03-file-modified-24h-ago.md)
- [Check file existence](./01-04-file-exists.md)

- [Cryptography]()

Expand Down Expand Up @@ -36,6 +37,7 @@

- [Explicit Threads](./07-01-spawn.md)
- [Shared Data](./07-02-shared-data.md)
- [Thread pool](./07-03-threadpool.md)

- [Operating System]()

Expand Down
2 changes: 1 addition & 1 deletion book-src/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

[Zig cookbook](https://github.com/zigcc/zig-cookbook) is a collection of simple Zig programs that demonstrate good practices to accomplish common programming tasks.

> - Main branch tracks Zig 0.13.0 and master, and ensured on Linux and macOS via GitHub actions.
> - Main branch tracks Zig 0.13.0 and master, and are tested on Linux and macOS via GitHub actions.
> - Zig 0.11.0 support can be found in [here](https://github.com/zigcc/zig-cookbook/tree/0.11.0).

# How to use
Expand Down
13 changes: 13 additions & 0 deletions src/01-04.zig
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});
}
27 changes: 27 additions & 0 deletions src/07-03.zig
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", .{});
}
Loading