mirror of
https://codeberg.org/ziglings/exercises.git
synced 2026-06-08 07:50:00 +00:00
improvements for async-io
This commit is contained in:
@@ -1180,9 +1180,9 @@ const exercises = [_]Exercise{
|
|||||||
.{
|
.{
|
||||||
.main_file = "093_async9.zig",
|
.main_file = "093_async9.zig",
|
||||||
.output =
|
.output =
|
||||||
\\Main thread continues...
|
\\Computing concurrently!
|
||||||
\\Computing on a separate thread!
|
\\Main continues...
|
||||||
\\Main thread done waiting.
|
\\Main done waiting.
|
||||||
\\Result: 123
|
\\Result: 123
|
||||||
, // pay attention to the comma
|
, // pay attention to the comma
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -5,22 +5,29 @@
|
|||||||
// The difference:
|
// The difference:
|
||||||
//
|
//
|
||||||
// io.async():
|
// io.async():
|
||||||
// * The function MAY run on another thread, or it may run
|
// * The function MAY run on a separate unit of concurrency,
|
||||||
// immediately on the current thread (synchronously).
|
// or it may run immediately on the caller (synchronously).
|
||||||
// * Never fails — if no thread is available, it just runs
|
// * Never fails — if no concurrency is available, it just
|
||||||
// the function right away.
|
// runs the function right away.
|
||||||
// * More portable, works with all Io backends.
|
// * More portable, works with all Io backends.
|
||||||
//
|
//
|
||||||
// io.concurrent():
|
// io.concurrent():
|
||||||
// * GUARANTEES a separate unit of concurrency (a real thread
|
// * GUARANTEES a separate unit of concurrency.
|
||||||
// in the Threaded backend).
|
|
||||||
// * Can fail with error.ConcurrencyUnavailable if resources
|
// * Can fail with error.ConcurrencyUnavailable if resources
|
||||||
// are exhausted or the backend doesn't support it.
|
// are exhausted or the backend doesn't support it.
|
||||||
// * Use when you NEED true parallelism.
|
// * Use when you NEED the task to run independently of the
|
||||||
|
// caller.
|
||||||
|
//
|
||||||
|
// What is a "unit of concurrency"? That depends on the backend!
|
||||||
|
// The Threaded backend uses OS threads. But the Evented backends
|
||||||
|
// (Uring, Kqueue, Dispatch) use M:N green threads / fibers,
|
||||||
|
// which can provide concurrency even on a SINGLE OS thread.
|
||||||
|
// Your code doesn't need to know the difference.
|
||||||
//
|
//
|
||||||
// Because concurrent() can fail, you must handle the error:
|
// Because concurrent() can fail, you must handle the error:
|
||||||
//
|
//
|
||||||
// var future = try io.concurrent(myFn, .{args});
|
// var future = try io.concurrent(myFn, .{args});
|
||||||
|
// defer _ = future.cancel(io);
|
||||||
// const result = future.await(io);
|
// const result = future.await(io);
|
||||||
//
|
//
|
||||||
// Notice the 'try' — that's the key difference in usage!
|
// Notice the 'try' — that's the key difference in usage!
|
||||||
@@ -33,25 +40,30 @@ const print = std.debug.print;
|
|||||||
pub fn main(init: std.process.Init) !void {
|
pub fn main(init: std.process.Init) !void {
|
||||||
const io = init.io;
|
const io = init.io;
|
||||||
|
|
||||||
// Launch with a guaranteed separate thread.
|
// Launch with a guaranteed separate unit of concurrency.
|
||||||
// Which Io method guarantees true concurrency?
|
// Which Io method guarantees this?
|
||||||
// (Hint: unlike io.async, this one can fail!)
|
// (Hint: unlike io.async, this one can fail!)
|
||||||
var future = try io.???(compute, .{io});
|
var future = try io.???(compute, .{io});
|
||||||
|
defer _ = future.cancel(io);
|
||||||
|
|
||||||
print("Main thread continues...\n", .{});
|
// Note: All breaks in this excercise (using sleep)
|
||||||
|
// are only necessary for a deterministic result.
|
||||||
// Wait 100 millisecond so the output order is deterministic.
|
|
||||||
io.sleep(std.Io.Duration.fromMilliseconds(100), .awake) catch {};
|
io.sleep(std.Io.Duration.fromMilliseconds(100), .awake) catch {};
|
||||||
|
|
||||||
print("Main thread done waiting.\n", .{});
|
print("Main continues...\n", .{});
|
||||||
|
|
||||||
|
// Wait 1 second for the output order.
|
||||||
|
io.sleep(std.Io.Duration.fromMilliseconds(200), .awake) catch {};
|
||||||
|
|
||||||
|
print("Main done waiting.\n", .{});
|
||||||
|
|
||||||
const result = future.await(io);
|
const result = future.await(io);
|
||||||
print("Result: {}\n", .{result});
|
print("Result: {}\n", .{result});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compute(io: std.Io) u32 {
|
fn compute(io: std.Io) u32 {
|
||||||
print("Computing on a separate thread!\n", .{});
|
print("Computing concurrently!\n", .{});
|
||||||
// Simulate some work.
|
// Simulate some work.
|
||||||
io.sleep(std.Io.Duration.fromMilliseconds(200), .awake) catch return 0;
|
io.sleep(std.Io.Duration.fromMilliseconds(400), .awake) catch return 0;
|
||||||
return 123;
|
return 123;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
--- exercises/093_async9.zig 2026-04-03 13:44:50.526780809 +0200
|
--- exercises/093_async9.zig 2026-04-06 19:26:11.388025362 +0200
|
||||||
+++ answers/093_async9.zig 2026-04-03 13:44:54.957870294 +0200
|
+++ answers/093_async9.zig 2026-04-06 19:18:36.242931688 +0200
|
||||||
@@ -36,7 +36,7 @@
|
@@ -43,7 +43,7 @@
|
||||||
// Launch with a guaranteed separate thread.
|
// Launch with a guaranteed separate unit of concurrency.
|
||||||
// Which Io method guarantees true concurrency?
|
// Which Io method guarantees this?
|
||||||
// (Hint: unlike io.async, this one can fail!)
|
// (Hint: unlike io.async, this one can fail!)
|
||||||
- var future = try io.???(compute, .{io});
|
- var future = try io.???(compute, .{io});
|
||||||
+ var future = try io.concurrent(compute, .{io});
|
+ var future = try io.concurrent(compute, .{io});
|
||||||
|
defer _ = future.cancel(io);
|
||||||
|
|
||||||
print("Main thread continues...\n", .{});
|
// Note: All breaks in this excercise (using sleep)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user