/// Spawns a new thread, and returns a join handle for it.
Spawns a new thread, and returns a join handle for it.
Spawns a new thread and returns a join handle for it .
pub fn spawn<F, T>(self, f: F) -> io::Result<ScopedJoinHandle<'scope, T>>
    where
        F: FnOnce(&Scope<'env>) -> T,
        F: Send + 'env,
        T: Send + 'env,
    {
        // The result of `f` will be stored here.
        let result = SharedOption::default();
        // Spawn the thread and grab its join handle and thread handle.
        let (handle, thread) = {
            let result = Arc::clone(&result);
            // A clone of the scope that will be moved into the new thread.
            let scope = Scope {
                handles: Arc::clone(&self.scope.handles),
                wg: self.scope.wg.clone(),
                _marker: PhantomData,
            };
            // Spawn the thread.
            let handle = {
                let closure = move || {
                    // Make sure the scope is inside the closure with the proper `'env` lifetime.
                    let scope: Scope<'env> = scope;
                    // Run the closure.
                    let res = f(&scope);
                    // Store the result if the closure didn't panic.
                    *result.lock().unwrap() = Some(res);
                };
                // Change the type of `closure` from `FnOnce() -> T` to `FnMut() -> T`.
                let mut closure = Some(closure);
                let closure = move || closure.take().unwrap()();
                // Allocate `clsoure` on the heap and erase the `'env` bound.
                let closure: Box<FnMut() + Send + 'env> = Box::new(closure);
                let closure: Box<FnMut() + Send + 'static> = unsafe {
                    mem::transmute(closure)
                };
                // Finally, spawn the closure.
                let mut closure = closure;
                self.builder.spawn(move || closure())?
            };
            let thread = handle.thread().clone();
            let handle = Arc::new(Mutex::new(Some(handle)));
            (handle, thread)
        };
        // Add the handle to the shared list of join handles.
        self.scope
            .handles
            .lock()
            .unwrap()
            .push(Arc::clone(&handle));
        Ok(ScopedJoinHandle {
            handle,
            result,
            thread,
            _marker: PhantomData,
        })
    }
pub fn spawn < F , T > ( self , f : F ) -> io :: Result < ScopedJoinHandle < ' scope , T > > where F : FnOnce ( & Scope < ' env > ) -> T , F : Send + ' env , T : Send + ' env , { // The result of `f` will be stored here. let result = SharedOption :: default ( ) ; // Spawn the thread and grab its join handle and thread handle. let ( handle , thread ) = { let result = Arc :: clone ( & result ) ; // A clone of the scope that will be moved into the new thread. let scope = Scope { handles : Arc :: clone ( & self . scope . handles ) , wg : self . scope . wg . clone ( ) , _marker : PhantomData , } ; // Spawn the thread. let handle = { let closure = move | | { // Make sure the scope is inside the closure with the proper `'env` lifetime. let scope : Scope < ' env > = scope ; // Run the closure. let res = f ( & scope ) ; // Store the result if the closure didn't panic. * result . lock ( ) . unwrap ( ) = Some ( res ) ; } ; // Change the type of `closure` from `FnOnce() -> T` to `FnMut() -> T`. let mut closure = Some ( closure ) ; let closure = move | | closure . take ( ) . unwrap ( ) ( ) ; // Allocate `clsoure` on the heap and erase the `'env` bound. let closure : Box < FnMut ( ) + Send + ' env > = Box :: new ( closure ) ; let closure : Box < FnMut ( ) + Send + ' static > = unsafe { mem :: transmute ( closure ) } ; // Finally, spawn the closure. let mut closure = closure ; self . builder . spawn ( move | | closure ( ) ) ? } ; let thread = handle . thread ( ) . clone ( ) ; let handle = Arc :: new ( Mutex :: new ( Some ( handle ) ) ) ; ( handle , thread ) } ; // Add the handle to the shared list of join handles. self . scope . handles . lock ( ) . unwrap ( ) . push ( Arc :: clone ( & handle ) ) ; Ok ( ScopedJoinHandle { handle , result , thread , _marker : PhantomData , } ) }
Spawns a new thread, and returns a join handle for it.
Spawns a new thread and returns a join handle for it .
self
f
F