Giter VIP home page Giter VIP logo

Comments (4)

notgull avatar notgull commented on June 10, 2024

schedule is called when the Waker is waked. It is only not Send + Sync for spawn_unchecked, which is unsafe and stipulates that the Waker not be waked on other threads.

from async-task.

Berrysoft avatar Berrysoft commented on June 10, 2024

Please point out if I'm wrong. I wrote a program like this:

use async_task::Runnable;
use send_wrapper::SendWrapper;
use std::{cell::RefCell, collections::VecDeque, future::poll_fn, rc::Rc, task::Poll};

fn main() {
    let runnables = Rc::new(RefCell::new(VecDeque::<Runnable>::new()));

    let schedule = {
        let runnables = SendWrapper::new(runnables.clone());
        move |r| runnables.borrow_mut().push_back(r)
    };

    let (runnable, task) = async_task::spawn_local(
        poll_fn(|cx| {
            cx.waker().wake_by_ref();
            Poll::<()>::Pending
        }),
        schedule,
    );
    let t = std::thread::spawn(move || smol::block_on(task));
    runnable.schedule();
    loop {
        let next_task = runnables.borrow_mut().pop_front();
        if let Some(r) = next_task {
            r.run();
        } else {
            break;
        }
    }
    t.join().unwrap();
}

It spawns a async block and waits the task in another thread. The schedule will panic if it's called in other threads. However, this program just blocks and never panics nor exits. Why?

from async-task.

notgull avatar notgull commented on June 10, 2024

The waker from the task on the main thread isn't being sent to the spawned thread. The waker from the block_on on the spawned thread is being sent to the main thread. The reason why the thread stops is because it's waiting on task forever. The loop breaks out before the task can be scheduled again.

from async-task.

Berrysoft avatar Berrysoft commented on June 10, 2024

Thank you! Now the problem is solved.

from async-task.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.