コンテンツにスキップ

portable-atomic — Crate 詳細

portable-atomic

Mature no_std

ターゲット差を吸収する portable な atomic 型を提供する crate。atomic 命令が弱い MCU や no_std 環境でも、critical-section などと組み合わせて上位 crate が安全に同期プリミティブを実装するための基盤になります。

Portable atomic types, including support for targets without native atomics.

portable-atomic は、アプリが直接使うだけでなく、static_cell など他 crate の内部依存としても重要です。ネイティブ atomic がないターゲットでは critical-section 実装の有無がリンクや動作に影響するため、依存ツリー全体での feature 確認が必要です。

バージョン
1.13.1
ライセンス
Apache-2.0 OR MIT
メンテナンス
活発に開発中

コード例

std::sync::atomic と近い感覚で使えますが、ターゲットごとの feature 設計が重要です。

AtomicUsize の利用
#![no_std]
use portable_atomic::{AtomicUsize, Ordering};
static COUNT: AtomicUsize = AtomicUsize::new(0);
pub fn next() -> usize {
COUNT.fetch_add(1, Ordering::Relaxed)
}

関連 Crates