#sync #reference-counting #arc #data-structures #weak-references

no-std parc

Rust crate for cancellable time based contracts in async/await

2 stable releases

1.0.1 Nov 8, 2019
1.0.0 Nov 5, 2019

⚠️ Issues reported

#20 in #datastructure

Download history 22/week @ 2024-02-22 86/week @ 2024-02-29 11/week @ 2024-03-07

119 downloads per month
Used in rustracts

MIT license

21KB
309 lines

parc Latest Version Rust Documentation

This crate exposes ParentArc<T> which is comparable to an Arc<T> but "strong" references cannot be cloned. This allows the ParentArc<T> to lock its weak references and block until all strong references are dropped. Once it is the only reference it can be consummed safely.

Usage

This crate is compatible with #![no_std] environnement that provides an allocator.

[dependencies]
parc = {version="1", default-features=false} # for no_std

Example

use parc::ParentArc;
use std::thread;
use std::sync;

fn main() {
	let m = ParentArc::new(sync::Mutex::new(0));

	let mut vh = Vec::new();
	for _ in 0..10 {
		let h = thread::spawn({
			let weak = ParentArc::downgrade(&m);
			move || loop {
				match weak.upgrade() {
					Some(mutex) => *mutex.lock().unwrap() += 1,
					None => break,
				}
			}
		});
		vh.push(h);
	}

	let _: sync::Mutex<usize> = m.block_into_inner();
	for h in vh {
		let _ = h.join();
	}
}

No runtime deps

Features