Coder Social home page Coder Social logo

async_mutex's Introduction

AsyncMutex

AsyncMutex is an asynchronous Mutex, used for synchronization in the context of Futures. A usual Mutex (from std) is not a good choice for synchronization between Futures, because it may block your future, and it will not be able to yield execution to another future.

AsyncMutex yields execution to a different future if the resource is not ready. When the resource is ready, the future waiting for the resource will be woken up.

Example:

#![feature(futures_api, pin, async_await, await_macro, arbitrary_self_types)]
#![feature(generators)]
use async_mutex::AsyncMutex;
use std::sync::Arc;
use futures::executor::ThreadPool;
use futures::task::SpawnExt;
use futures::{future, SinkExt, StreamExt};
use futures::channel::mpsc;

struct NumCell {
    x: u32,
}

impl NumCell {
    async fn add(&mut self) {
        await!(future::lazy(|_| {
            self.x += 1
        }));
    }
}

async fn add_task(arc_mut_num_cell: Arc<AsyncMutex<NumCell>>, mut sender: mpsc::Sender<()>) {
    let mut guard = await!(arc_mut_num_cell.lock());
    await!(guard.add());
    await!(sender.send(())).unwrap();
}

fn main() {
    let (sender, mut receiver) = mpsc::channel::<()>(0);
    let arc_mut_num_cell = Arc::new(AsyncMutex::new(NumCell {x: 0}));
    let mut thread_pool = ThreadPool::new().unwrap();
    for _ in 0 .. 100usize {
        thread_pool.spawn(add_task(arc_mut_num_cell.clone(), sender.clone()));
    }
    thread_pool.run(async move {
        for _ in 0 .. 100usize {
            await!(receiver.next()).unwrap();
        }
        let mut guard = await!(arc_mut_num_cell.lock());
        assert_eq!(guard.x, 100);
    });
}

async_mutex's People

Contributors

juchiast avatar kamyuentse avatar realcr avatar

Watchers

 avatar  avatar

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.