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

Add 07-01 Concurrency #8

Merged
merged 4 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 49 additions & 0 deletions book-src/07-01-spawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## Spawn a short-lived thread

The example uses [`std.Thread`] for concurrent and parallel programming.
[`std.Thread.spawn`] spawns a new thread to calculate the result.

This example splits the array in half and performs the work in separate threads.

```zig
const std = @import("std");

pub fn main() !void {
var arr: [4]i32 = .{ 1, 25, -4, 10 };
var max_value: i32 = undefined;
try findMax(&max_value, &arr);
std.debug.assert(max_value == 25);
}

fn findMax(max_value: *i32, values: []i32) !void {
const THRESHOLD: usize = 2;

if (values.len == 0) unreachable;

jiacai2050 marked this conversation as resolved.
Show resolved Hide resolved
if (values.len <= THRESHOLD) {
var res = values[0];
for (values) |it| {
res = @max(res, it);
}
max_value.* = res;
return;
}

const mid = values.len / 2;
const left = values[0..mid];
const right = values[mid..];

var v1: i32 = undefined;
const t1 = try std.Thread.spawn(.{}, findMax, .{ &v1, left });
t1.join();

var v2: i32 = undefined;
const t2 = try std.Thread.spawn(.{}, findMax, .{ &v2, right });
t2.join();

max_value.* = @max(v1, v2);
}
```

[`std.Thread`]: https://ziglang.org/documentation/0.11.0/std/#A;std:Thread
[`std.Thread.spawn`]: https://ziglang.org/documentation/0.11.0/std/#A;std:Thread.spawn
3 changes: 3 additions & 0 deletions book-src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@

- [Algorithms]()
- [Generate Random Values](./06-01-rand.md)

- [Concurrency]()
- [Explicit Threads](./07-01-spawn.md)
37 changes: 37 additions & 0 deletions src/07-01.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const std = @import("std");

pub fn main() !void {
var arr: [4]i32 = .{ 1, 25, -4, 10 };
var max_value: i32 = undefined;
try findMax(&max_value, &arr);
std.debug.assert(max_value == 25);
}

fn findMax(max_value: *i32, values: []i32) !void {
const THRESHOLD: usize = 2;

if (values.len == 0) unreachable;
jiacai2050 marked this conversation as resolved.
Show resolved Hide resolved

jiacai2050 marked this conversation as resolved.
Show resolved Hide resolved
if (values.len <= THRESHOLD) {
var res = values[0];
for (values) |it| {
res = @max(res, it);
}
max_value.* = res;
return;
}

const mid = values.len / 2;
const left = values[0..mid];
const right = values[mid..];

var v1: i32 = undefined;
const t1 = try std.Thread.spawn(.{}, findMax, .{ &v1, left });
t1.join();

var v2: i32 = undefined;
const t2 = try std.Thread.spawn(.{}, findMax, .{ &v2, right });
t2.join();

max_value.* = @max(v1, v2);
}