How to share limited resources between simultaneously running tasks using Rust futures?

I am implementing a system that processes certain types of tasks asynchronously in Rust. To do this, I use a futures box (beta 0.2).

Performing one of these tasks requires a temporary but exclusive use of a certain type of resource, of which a fixed amount exists in the system.

Keep in mind that all this happens in a parallel context: multiple threads may try to process the task at the same time. The simplified version of the interface that I used is as follows:

// A fixed number of these are held by the System, and a TaskFuture needs one to complete.
pub struct Resource;

pub struct System {
    // how should the system hold Resources?
}

impl System {
    pub fn queue_task(&self, data: TaskData) -> TaskFuture {
        TaskFuture { system: self }
    }
}

pub struct TaskFuture<'a> {
    system: &'a System,
}

impl<'a> Future for TaskFuture<'a> {
    type Item = ?;
    type Error = ?;
    fn poll(&mut self, cx: &mut Context) -> Result<Async<Self::Item>, Self::Error> {
        /* how can I implement this? */
    }
}

As you can see, I'm not quite sure what is the best way to implement such a system.

, System TaskFuture::poll Mutex<Resource>, TaskFuture::poll , , , NotReady ( , ).

, , , , std futures -based, , futures -based?

+4

Source: https://habr.com/ru/post/1695633/


All Articles