#data #lock #initializer #deprecated #sync #spin #static

deprecated nightly spinlock

DEPRECATED! Use crate spin instead. A simple spinlock. It may contain data, is usable without std and there is a static initializer available

3 releases

Uses old Rust 2015

0.1.2 Dec 14, 2015
0.1.1 Jan 13, 2015
0.1.0 Jan 13, 2015

#9 in #initializer

MIT license

7KB
69 lines

spinlock-rs

Build Status

Documentation

This Rust library implements a simple spinlock.

Build

Just run cargo build.

Usage

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

extern crate spinlock;
use spinlock::Spinlock;

fn main()
{
    let spinlock = Spinlock::new(0);

    // Modify the data
    {
      let mut data = spinlock.lock();
      *data = 2;
    }

    // Read the data
    let answer =
    {
      let data = spinlock.lock();
      *data
    };

    println!("Answer is {}", answer);
}

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

Remarks

The behaviour of this lock is similar to that of std::sync::Mutex. It differs on the following:

  • The lock will not be poisoned in case of failure;
  • The lock can also be used from a plain thread (such as a bare pthread).

No runtime deps