Code Monkey home page Code Monkey logo

spin-rs's Introduction

spin-rs

Build Status Crates.io version

Documentation

This Rust library implements a simple spinlock.

Usage

By default this crate only works on nightly but you can disable the default features if you want to run on stable. Nightly is more efficient than stable currently.

Also, Once is only available on nightly as it is only useful when const_fn is available.

Include the following code in your Cargo.toml

[dependencies.spin]
version = "0.3"
# If you want to run on stable you will need to add the following:
# default-features = false

Example

When calling lock on a Mutex you will get a reference to the data. When this reference is dropped, the lock will be unlocked.

extern crate spin;

fn main()
{
    let mutex   = spin::Mutex::new(0);
    let rw_lock = spin::RwLock::new(0);

    // Modify the data
    {
      let mut data = mutex.lock();
      *data = 2;
      let mut data = rw_lock.write();
      *data = 3;
    }

    // Read the data
    let answer =
    {
      let data1 = mutex.lock();
      let data2 = rw_lock.read();
      let data3 = rw_lock.read(); // sharing
      (*data1, *data2, *data3)
    };

    println!("Answers are {:?}", answer);
}

To share the lock, an Arc<Mutex<T>> may be used.

Remarks

The behaviour of these lock is similar to their namesakes in std::sync. they differ on the following:

  • The lock will not be poisoned in case of failure;

spin-rs's People

Contributors

mvdnes avatar ericson2314 avatar ryman avatar arthurprs avatar phil-opp avatar dpc avatar birkenfeld avatar

Watchers

James Cloos avatar Burgos 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.