pub struct Scope<'env> {
handles: Arc<Mutex<Vec<Arc<Mutex<Option<JoinHandle<()>>>>>>>,
wait_group: WaitGroup,
_marker: PhantomData<&'env mut &'env ()>,
}
Expand description
A scope for spawning threads.
Fields§
§handles: Arc<Mutex<Vec<Arc<Mutex<Option<JoinHandle<()>>>>>>>
§wait_group: WaitGroup
§_marker: PhantomData<&'env mut &'env ()>
Implementations§
§impl<'env> Scope<'env>
impl<'env> Scope<'env>
pub fn spawn<'scope, F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T>
pub fn spawn<'scope, F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T>
Spawns a scoped thread.
This method is similar to the spawn
function in Rust’s standard library. The difference
is that this thread is scoped, meaning it’s guaranteed to terminate before the scope exits,
allowing it to reference variables outside the scope.
The scoped thread is passed a reference to this scope as an argument, which can be used for spawning nested threads.
The returned handle can be used to manually join the thread before the scope exits.
This will create a thread using default parameters of ScopedThreadBuilder
, if you want to specify the
stack size or the name of the thread, use this API instead.
§Panics
Panics if the OS fails to create a thread; use ScopedThreadBuilder::spawn
to recover from such errors.
§Examples
use crossbeam_utils::thread;
thread::scope(|s| {
let handle = s.spawn(|_| {
println!("A child thread is running");
42
});
// Join the thread and retrieve its result.
let res = handle.join().unwrap();
assert_eq!(res, 42);
}).unwrap();
pub fn builder<'scope>(&'scope self) -> ScopedThreadBuilder<'scope, 'env>
pub fn builder<'scope>(&'scope self) -> ScopedThreadBuilder<'scope, 'env>
Creates a builder that can configure a thread before spawning.
§Examples
use crossbeam_utils::thread;
thread::scope(|s| {
s.builder()
.spawn(|_| println!("A child thread is running"))
.unwrap();
}).unwrap();
Trait Implementations§
Auto Trait Implementations§
impl<'env> Freeze for Scope<'env>
impl<'env> RefUnwindSafe for Scope<'env>
impl<'env> Send for Scope<'env>
impl<'env> Unpin for Scope<'env>
impl<'env> !UnwindSafe for Scope<'env>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request