Coder Social home page Coder Social logo

sunsided / rendezvous-rs Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 39 KB

Easier rendezvous channels for thread synchronization

Home Page: https://crates.io/crates/rendezvous

License: European Union Public License 1.2

Rust 100.00%
rendezvous rust thread thread-synchronization

rendezvous-rs's Introduction

Easier Rendezvous Channels

In rust, mpsc::channel can be used as a synchronization primitive between threads by utilizing the fact that we can block on the receiver's recv() function until all senders are dropped.

This crate aims at giving the concept an expressive name and at reducing some classes of race conditions, namely those where the original sender was not dropped before the call to recv().

This version of the crate only supports synchronous code due to the dropping semantics.

cargo add rendezvous

Example usage

use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
use rendezvous::{Rendezvous, RendezvousGuard};

/// A slow worker function. Sleeps, then mutates a value.
fn slow_worker_fn(_guard: RendezvousGuard, mut value: Arc<Mutex<u32>>) {
    thread::sleep(Duration::from_millis(400));
    let mut value = value.lock().unwrap();
    *value = 42;
}

fn example() {
    // The guard that ensures synchronization across threads.
    // Rendezvous itself acts as a guard: If not explicitly dropped, it will block the current
    // scope until all rendezvous points are reached.
    let rendezvous = Rendezvous::new();

    // A value to mutate in a different thread.
    let value = Arc::new(Mutex::new(0u32));

    // Run the worker in a thread.
    thread::spawn({
        let guard = rendezvous.fork_guard();
        let value = value.clone();
        move || slow_worker_fn(guard, value)
    });

    // Block until the thread has finished its work.
    rendezvous.rendezvous();

    // The thread finished in time.
    assert_eq!(*(value.lock().unwrap()), 42);
}

rendezvous-rs's People

Contributors

dependabot[bot] avatar sunsided 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.