about summary refs log tree commit diff
path: root/library/std/src/thread/scoped.rs
AgeCommit message (Collapse)AuthorLines
2025-04-27use generic Atomic type where possibleChristopher Durham-3/+3
in core/alloc/std only for now, and ignoring test files Co-authored-by: Pavel Grigorenko <GrigorenkoPV@ya.ru>
2024-11-25Rollup merge of #132730 - joboet:after_main_sync, r=Noratrieb许杰友 Jieyou Xu (Joe)-3/+4
std: allow after-main use of synchronization primitives By creating an unnamed thread handle when the actual one has already been destroyed, synchronization primitives using thread parking can be used even outside the Rust runtime. This also fixes an inefficiency in the queue-based `RwLock`: if `thread::current` was not initialized yet, it will create a new handle on every parking attempt without initializing `thread::current`. The private `current_or_unnamed` function introduced here fixes this.
2024-11-22Fix typo in `std::thread::Scope::spawn` documentation.Colin Finck-1/+1
2024-11-18std: allow after-main use of synchronization primitivesjoboet-3/+4
By creating an unnamed thread handle when the actual one has already been destroyed, synchronization primitives using thread parking can be used even outside the Rust runtime. This also fixes an inefficiency in the queue-based `RwLock`: if `thread::current` was not initialized yet, it will create a new handle on every parking attempt without initializing `thread::current`. The private `current_or_unnamed` function introduced here fixes this.
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-3/+3
2024-07-29Reformat `use` declarations.Nicholas Nethercote-2/+1
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-07-26Fix doc nitsJohn Arundel-1/+1
Many tiny changes to stdlib doc comments to make them consistent (for example "Returns foo", rather than "Return foo", per RFC1574), adding missing periods, paragraph breaks, backticks for monospace style, and other minor nits. https://github.com/rust-lang/rfcs/blob/master/text/1574-more-api-documentation-conventions.md#appendix-a-full-conventions-text
2024-04-11Move rare overflow error to a cold functionKornel-2/+8
2024-02-24Fix incorrect doc of ScopedJoinHandle::is_finishedWojciech Geisler-1/+1
Fixes the explanation how to use is_finished to achieve a non-blocking join. The updated version matches the documentation of the non-scoped JoinHandle::is_finished.
2022-12-05fix dupe word typosRageking8-1/+1
2022-06-27fix data race in thread::scopeRalf Jung-4/+6
2022-06-20Improve docs for `is_running` to explain use caseJosh Triplett-3/+4
2022-06-11Stabilize scoped threads.Mara Bos-8/+10
2022-03-30Don't stabilize ScopedJoinHandle::is_finished yet.Mara Bos-1/+0
2022-03-19Stabilize thread::is_finishedJubilee Young-1/+1
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-1/+1
This updates the standard library's documentation to use the new syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored).
2022-03-09Add documentation about lifetimes to thread::scope.Mara Bos-0/+18
2022-03-08Rollup merge of #94559 - m-ou-se:thread-scope-spawn-closure-without-arg, ↵Matthias Krüger-21/+23
r=Mark-Simulacrum Remove argument from closure in thread::Scope::spawn. This implements ```@danielhenrymantilla's``` [suggestion](https://github.com/rust-lang/rust/issues/93203#issuecomment-1040798286) for improving the scoped threads interface. Summary: The `Scope` type gets an extra lifetime argument, which represents basically its own lifetime that will be used in `&'scope Scope<'scope, 'env>`: ```diff - pub struct Scope<'env> { .. }; + pub struct Scope<'scope, 'env: 'scope> { .. } pub fn scope<'env, F, T>(f: F) -> T where - F: FnOnce(&Scope<'env>) -> T; + F: for<'scope> FnOnce(&'scope Scope<'scope, 'env>) -> T; ``` This simplifies the `spawn` function, which now no longer passes an argument to the closure you give it, and now uses the `'scope` lifetime for everything: ```diff - pub fn spawn<'scope, F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T> + pub fn spawn<F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T> where - F: FnOnce(&Scope<'env>) -> T + Send + 'env, + F: FnOnce() -> T + Send + 'scope, - T: Send + 'env; + T: Send + 'scope; ``` The only difference the user will notice, is that their closure now takes no arguments anymore, even when spawning threads from spawned threads: ```diff thread::scope(|s| { - s.spawn(|_| { + s.spawn(|| { ... }); - s.spawn(|s| { + s.spawn(|| { ... - s.spawn(|_| ...); + s.spawn(|| ...); }); }); ``` <details><summary>And, as a bonus, errors get <em>slightly</em> better because now any lifetime issues point to the outermost <code>s</code> (since there is only one <code>s</code>), rather than the innermost <code>s</code>, making it clear that the lifetime lasts for the entire <code>thread::scope</code>. </summary> ```diff error[E0373]: closure may outlive the current function, but it borrows `a`, which is owned by the current function --> src/main.rs:9:21 | - 7 | s.spawn(|s| { - | - has type `&Scope<'1>` + 6 | thread::scope(|s| { + | - lifetime `'1` appears in the type of `s` 9 | s.spawn(|| println!("{:?}", a)); // might run after `a` is dropped | ^^ - `a` is borrowed here | | | may outlive borrowed value `a` | note: function requires argument type to outlive `'1` --> src/main.rs:9:13 | 9 | s.spawn(|| println!("{:?}", a)); // might run after `a` is dropped | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: to force the closure to take ownership of `a` (and any other referenced variables), use the `move` keyword | 9 | s.spawn(move || println!("{:?}", a)); // might run after `a` is dropped | ++++ " ``` </details> The downside is that the signature of `scope` and `Scope` gets slightly more complex, but in most cases the user wouldn't need to write those, as they just use the argument provided by `thread::scope` without having to name its type. Another downside is that this does not work nicely in Rust 2015 and Rust 2018, since in those editions, `s` would be captured by reference and not by copy. In those editions, the user would need to use `move ||` to capture `s` by copy. (Which is what the compiler suggests in the error.)
2022-03-07Use `f` instead of `|| f()`.Mara Bos-1/+1
Co-authored-by: Mark Rousskov <mark.simulacrum@gmail.com>
2022-03-04Use '_ for irrelevant lifetimes in Debug impl.Mara Bos-1/+1
Co-authored-by: Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>
2022-03-03Remove argument from closure in thread::Scope::spawn.Mara Bos-21/+23
2022-03-03Remove unnecessary #![feature]s from doctest.Mara Bos-2/+0
2022-03-03Rename JoinHandle::is_running to is_finished and update docs.Mara Bos-4/+9
Co-authored-by: Josh Triplett <josh@joshtriplett.org>
2022-01-22Add test for thread::Scope invariance.Mara Bos-0/+13
2022-01-05Mention *scoped* thread in panic message.Mara Bos-1/+1
Co-authored-by: Amanieu d'Antras <amanieu@gmail.com>
2022-01-05Note the invariance over 'env in Scope<'env>.Mara Bos-0/+2
2022-01-05Fix missing .load() in Scope's Debug impl.Mara Bos-1/+1
2022-01-05Rename n_running_threads to num_running_threads.Mara Bos-9/+9
2022-01-04Fix typo in Scope::spawn docs.Mara Bos-1/+1
Co-authored-by: deltragon <m@dafert.at>
2022-01-04Fix typo in is_running() docs.Mara Bos-1/+1
Co-authored-by: Mattias Buelens <649348+MattiasBuelens@users.noreply.github.com>
2022-01-04Fix typo in documentation.Mara Bos-1/+1
2022-01-04Use > rather than == for overflow check in scoped threads.Mara Bos-1/+1
Co-authored-by: Jacob Lifshay <programmerjake@gmail.com>
2022-01-04Fix variance of thread::Scope.Mara Bos-1/+1
2022-01-04Formatting.Mara Bos-1/+3
2022-01-04Simplify panicking mechanism of thread::scope.Mara Bos-23/+17
It now panic!()s on its own, rather than resume_unwind'ing the panic payload from the thread. Using resume_unwind skips the panic_handler, meaning that the main thread would never have a panic handler run, which can get confusing.
2022-01-04Add documentation for scoped threads.Mara Bos-11/+175
2022-01-04Add ScopedJoinHandle::is_running().Mara Bos-1/+10
2022-01-04Implement RFC 3151: Scoped threads.Mara Bos-0/+132