Skip to content

Commit 199ae2d

Browse files
authored
docs: update @spawn example
1 parent 8b8d6aa commit 199ae2d

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -544,14 +544,15 @@ function process_data(x, y, z)
544544
@own x, y
545545
@own :mut z
546546

547-
@own tasks = [
548-
Threads.@spawn(@bc(foo(z))),
549-
Threads.@spawn(@bc(foo(z)))
550-
]
551-
sum(map(fetch, @take!(tasks)))
547+
@lifetime lt begin
548+
@ref ~lt r = z
549+
tasks = [
550+
BorrowChecker.@spawn(foo(r)),
551+
BorrowChecker.@spawn(foo(r)),
552+
]
553+
sum(fetch, tasks)
554+
end
552555
end
553556
```
554557

555558
Because we modified `foo` to accept `OrBorrowed{Bar{T}}`, we can safely pass immutable references to `z`, and it will _not_ be marked as moved in the original context! Immutable references are safe to pass in a multi-threaded context, so this doubles as a good way to prevent unintended thread races.
556-
557-
Note that this will nicely handle the case of multiple mutable references—if we had written `@bc foo(@mut(z))` while the other thread was running, we would see a `BorrowRuleError` because `z` is already mutably borrowed!

0 commit comments

Comments
 (0)