Closed
Description
Feature gate: #![feature(scoped_threads)]
This is a tracking issue for scoped threads.
RFC: https://rust-lang.github.io/rfcs/3151-scoped-threads.html
Example usage
let local_var = vec![1, 2, 3];
thread::scope(|s| {
s.spawn(|| println!("borrowed from thread #1: {:?}", local_var));
s.spawn(|| println!("borrowed from thread #2: {:?}", local_var));
println!("borrowed from the main thread: {:?}", local_var);
});
Public API
Documentation: https://doc.rust-lang.org/nightly/std/thread/fn.scope.html
// std::thread
pub fn scope<'env, F, T>(f: F) -> T
where
F: for<'scope> FnOnce(&'scope Scope<'scope, 'env>) -> T;
pub struct Scope<'scope, 'env: 'scope> { ... }
impl<'scope, 'env> Scope<'scope, 'env> {
pub fn spawn<F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T>
where
F: FnOnce() -> T + Send + 'scope,
T: Send + 'scope;
}
impl Builder {
pub fn spawn_scoped<'scope, 'env, F, T>(
self,
scope: &'scope Scope<'env>,
f: F,
) -> io::Result<ScopedJoinHandle<'scope, T>>
where
F: FnOnce() -> T + Send + 'scope,
T: Send + 'scope;
}
pub struct ScopedJoinHandle<'scope, T> { ... }
impl<'scope, T> ScopedJoinHandle<'scope, T> {
pub fn join(self) -> Result<T>;
pub fn thread(&self) -> &Thread;
pub fn is_finshed(&self) -> bool;
}
Steps / History
- RFC attempt 1: Add scoped threads to the standard library rfcs#2647
- RFC attempt 2: Scoped threads in the standard library, take 2 rfcs#3151
- Implementation: Implement RFC 3151: Scoped threads. #92555
- Change signatures a bit to remove the argument to the spawn closures: Remove argument from closure in thread::Scope::spawn. #94559
- Fix soundness issue in implementation: Fix soundness issue in scoped threads. #94644
- Document lifetimes: Add documentation about lifetimes to thread::scope. #94763
- Final comment period (FCP): Tracking Issue for scoped threads #93203 (comment)
- Stabilization PR: Stabilize scoped threads. #97992
Unresolved Questions
- Can we omit the
&Scope
argument to the functions given to.spawn()
? That is,scope.spawn(|| ..)
rather thanscope.spawn(|_| ..)
.- It's already possible by forcing the user to use
move || ..
instead, but that's not great. Maybe the language could be subtly changed to capture references or certainCopy
types by value rather than by reference(-to-reference). - See also this comment and the collapsed section in the comment below.
- Mostly answered in this comment.
- Working idea in this comment.
- Implementation in Remove argument from closure in thread::Scope::spawn. #94559
- It's already possible by forcing the user to use
- How to document the
'env
and'scope
lifetimes clearly without scaring people away.
Metadata
Metadata
Assignees
Labels
Area: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflowsCategory: An issue tracking the progress of sth. like the implementation of an RFCCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Relevant to the library API team, which will review and decide on the PR/issue.This issue / PR is in PFCP or FCP with a disposition to merge it.The final comment period is finished for this PR / Issue.Marks issues that should be documented in the release notes of the next release.