Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
What is std::sync::atomic
Search
Kentaro Matsumoto
September 03, 2019
Programming
0
160
What is std::sync::atomic
Rustのstd::sync::atomicの必要性と既に定義された型にトレイトを実装する方法を解説しました。
Kentaro Matsumoto
September 03, 2019
Tweet
Share
More Decks by Kentaro Matsumoto
See All by Kentaro Matsumoto
Marpを使って登壇資料を作る
matsu7874
0
930
Generate a rust client code by OpenAPI Generator
matsu7874
0
430
ざっと理解するRust 2024 Edition
matsu7874
0
1.2k
プリントデバッグを失敗させないテクニック
matsu7874
1
360
社外を巻き込んだ勉強会を定期開催するコツ
matsu7874
0
180
actix-webを使った開発のハマリポイントを避けたい
matsu7874
0
1k
our test strategy on actix-web app
matsu7874
0
1.5k
roadmap to rust 2024
matsu7874
0
2k
Rust tutorial for Pythonista
matsu7874
2
1.3k
Other Decks in Programming
See All in Programming
Browser and UI #2 HTML/ARIA
ken7253
2
180
AIコーディングの本質は“コード“ではなく“構造“だった / The essence of AI coding is not “code” but "structure
seike460
PRO
1
230
2025-04-25 GitHub Copilot Agent ライブデモ(スクリプト)
goataka
0
110
Golangci-lint v2爆誕: 君たちはどうすべきか
logica0419
1
250
最速Green Tea 🍵 Garbage Collector
kuro_kurorrr
1
110
Cursorを活用したAIプログラミングについて 入門
rect
0
190
音声プラットフォームのアーキテクチャ変遷から学ぶ、クラウドネイティブなバッチ処理 (20250422_CNDS2025_Batch_Architecture)
thousanda
0
410
エンジニアが挑む、限界までの越境
nealle
1
330
状態と共に暮らす:ステートフルへの挑戦
ypresto
3
1.2k
大LLM時代にこの先生きのこるには-ITエンジニア編
fumiyakume
8
3.3k
Ruby で作る RISC-V CPU エミュレーター / RISC-V CPU emulator made with Ruby
hayaokimura
5
1k
파급효과: From AI to Android Development
l2hyunwoo
0
160
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
34
2.2k
Scaling GitHub
holman
459
140k
Stop Working from a Prison Cell
hatefulcrawdad
268
20k
The Art of Programming - Codeland 2020
erikaheidi
54
13k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
8
700
Git: the NoSQL Database
bkeepers
PRO
430
65k
Unsuck your backbone
ammeep
671
58k
Intergalactic Javascript Robots from Outer Space
tanoku
271
27k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Designing for humans not robots
tammielis
253
25k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.5k
Transcript
What is std::sync::atomic 2019/09/13 Shinjuku.rs #6 @FORCIA
自己紹介 • 松本健太郎 ◦ @matsu7874 • フォルシア株式会社 ◦ Webエンジニア ◦
業務でRustを書いている
目次 • 闇雲にデータを共有すると何が起こるのか • Atomicとは • cmpトレイトを実装した
スレッド間でデータを共有すると 何が起こるのか
メモリの様子を拡大してみると 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
シングルスレッドの場合 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 11111111
11111111 11111111 11111111 11111111 11111111 11111111 11111111 書きます!
シングルスレッドの場合 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 読みます!
シングルスレッドの場合 何の問題も無い
マルチスレッドの場合 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 11111111
11111111 00000000 00000000 00000000 00000000 00000000 00000000 書きます!
マルチスレッドの場合 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 11111111
11111111 11111111 11111111 00000000 00000000 00000000 00000000 書いていますよ! 読みます!
マルチスレッドの場合 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 11111111
11111111 11111111 11111111 11111111 11111111 00000000 00000000 まだ書いていますヽ (`Д´)ノプンプン 読了
マルチスレッドの場合 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 11111111
11111111 11111111 11111111 11111111 11111111 00000000 00000000 まだ書いていますヽ (`Д´)ノプンプン 読了 は何を 読んだのか
マルチスレッドの場合 • 同じ値への読み書きが同時に発生すると困る事がある • 対応策 ◦ 操作が不可分にする ◦ 同じ値に操作する数を1にする
Atomicとは
Atomicとは? • 書き込み途中に読まれない • 読み込み途中に書かれない • アーキテクチャによって強い・弱いがあるらしい
std::sync::atomic • Atomic型を提供するモジュール ◦ AtomicBool, AtomicIsize, AtomicIsize • Atomic型はSyncトレイトが実装されている ◦
安全にスレッド間で共有可能 • スレッド間で共有される機能は持っていないのでArcを使う ◦ Arc::new(AtomicUsize::new(0))
Atomic以外の方法はあるの? • Mutex、RWLockがある • それぞれのパフォーマンスは ◦ "Performance Comparison of Mutex,
RWLock and Atomic types in Rust" というスライドが有名
比較を実装した
AtomicUsizeとAtomicUsizeを比較したい • 比較したいときはstd::cmp::PartialEqをimplする。 • AtomicUsizeはstd::sync::atomic::AtomicUsize。 • error[E0117]: only traits defined
in the current crate can be implemented for arbitrary types ◦ 現在のcrateで定義されたtraitだけを任意の型にimpl可能 ◦ どちらも既にあるのでimplできない。
newtypeパターン • 特に何もせずに新しい型だよって言っちゃう ◦ struct Bar(Foo); ◦ https://github.com/rust-unofficial/patterns/blob/master/patterns/newtype.md ◦ 自分で定義した型になら既にあるtraitが実装できる!
None
まとめ • スレッド間でデータ共有したい場合はそれ用の型を使う • Atomicが高速 • new typeパターンを覚えよう