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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Kentaro Matsumoto
September 03, 2019
Programming
0
190
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
claude_code.pdf
matsu7874
5
7.4k
Marpを使って登壇資料を作る
matsu7874
0
2k
Generate a rust client code by OpenAPI Generator
matsu7874
0
670
ざっと理解するRust 2024 Edition
matsu7874
0
1.8k
プリントデバッグを失敗させないテクニック
matsu7874
1
450
社外を巻き込んだ勉強会を定期開催するコツ
matsu7874
0
220
actix-webを使った開発のハマリポイントを避けたい
matsu7874
0
1.2k
our test strategy on actix-web app
matsu7874
0
1.7k
roadmap to rust 2024
matsu7874
0
2.2k
Other Decks in Programming
See All in Programming
Go 1.26でのsliceのメモリアロケーション最適化 / Go 1.26 リリースパーティ #go126party
mazrean
1
320
Event Storming
hschwentner
3
1.3k
「やめとこ」がなくなった — 1月にZennを始めて22本書いた AI共創開発のリアル
atani14
0
340
CSC307 Lecture 15
javiergs
PRO
0
200
AIに仕事を丸投げしたら、本当に楽になれるのか
dip_tech
PRO
0
170
20260228_JAWS_Beginner_Kansai
takuyay0ne
5
410
atmaCup #23でAIコーディングを活用した話
ml_bear
4
720
TROCCOで実現するkintone+BigQueryによるオペレーション改善
ssxota
0
120
CSC307 Lecture 10
javiergs
PRO
1
690
AWS Infrastructure as Code の新機能 2025 総まとめ 〜SA 4人による怒涛のデモ祭り〜
konokenj
10
3k
Claude Code、ちょっとした工夫で開発体験が変わる
tigertora7571
0
190
RAGでハマりがちな"Excelの罠"を、データの構造化で突破する
harumiweb
8
2.2k
Featured
See All Featured
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
1.8k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
The Language of Interfaces
destraynor
162
26k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
エンジニアに許された特別な時間の終わり
watany
106
240k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.7k
Side Projects
sachag
455
43k
Designing Powerful Visuals for Engaging Learning
tmiket
0
250
Ethics towards AI in product and experience design
skipperchong
2
210
A Soul's Torment
seathinner
5
2.4k
We Are The Robots
honzajavorek
0
190
[SF Ruby Conf 2025] Rails X
palkan
2
800
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パターンを覚えよう