SkillAgentSearch skills...

Spinlock.rs

A spinlocks implementation in rust

Install / Use

/learn @jorisgio/Spinlock.rs
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

spinlock-rs

A spinlock implementation in rust

documentation

Build

Run cargo build

Usage

The library implements a Reader/Writer lock. When locking a spin lock for shared Read access, you will get a reference to the protected data, and while locking for an exclusive Write access, you will get a mutable reference.

extern crate spinlock;
use spinlock::SpinLock;

fn main() {
	let spin = SpinLock::new(0);

	// Write access
	{
		let mut data = spin.write().unwrap();
		*data += 1;
	}
	// Read access
	{
		let data = spin.read().unwrap();
		println!("{}", *data);
	}
}

Please note that the spinlock doesn't deal itself with reference counting. You might want to use Arc<SpinLock<T>> to share the lock between threads.

Credits

The implementation is derived from the spinlock implementation written by Matt Dillon for DragonFlyBSD

Related Skills

View on GitHub
GitHub Stars8
CategoryDevelopment
Updated3y ago
Forks0

Languages

Rust

Security Score

70/100

Audited on Dec 26, 2022

No findings