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
230
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
What is std::sync::atomic
Rustのstd::sync::atomicの必要性と既に定義された型にトレイトを実装する方法を解説しました。
Kentaro Matsumoto
September 03, 2019
More Decks by Kentaro Matsumoto
See All by Kentaro Matsumoto
AIで仕事のやり方を変える
matsu7874
1
280
claude_code.pdf
matsu7874
5
7.7k
Marpを使って登壇資料を作る
matsu7874
0
2.4k
Generate a rust client code by OpenAPI Generator
matsu7874
0
790
ざっと理解するRust 2024 Edition
matsu7874
0
1.9k
プリントデバッグを失敗させないテクニック
matsu7874
1
500
社外を巻き込んだ勉強会を定期開催するコツ
matsu7874
0
250
actix-webを使った開発のハマリポイントを避けたい
matsu7874
0
1.3k
our test strategy on actix-web app
matsu7874
0
1.8k
Other Decks in Programming
See All in Programming
Go を使い始めて 2 ヶ月の学び / My first two months with Go
contour_gara
0
420
リアルな遅延を測る仕様
kota_yata
1
140
週末にAI-DLCを本気で回したら$1,600溶けた
hbashimizu
0
120
不幸な GC
chencmd
0
880
typoなんかねぇよ
raspython3
0
660
【デモ】Kiroで体験する仕様駆動開発|設計からコーディングまでAIと進める開発フロー
cmkudo
0
570
DynamoDBの基礎を振り返りながらベクトル検索機能を理解する
musan
3
260
信頼性の目標を誰も求めてない
shubox
0
490
Gmail/Google DriveをトリガーにAIエージェントを動かそう! / Run AI agents with Gmail/Google Drive as triggers!
har1101
3
460
思考垂れ流し開発 ~音声入力 × AIエージェント × 開発ハーネスによる試行錯誤~
npostring
0
810
Pythonの実行はどこまで賢くなったのか? CPythonとPyPyから見る最適化のしくみ
curekoshimizu
4
2.8k
Hono + Inertia + React で LP を構築した話
oukayuka
2
210
Featured
See All Featured
Building AI with AI
inesmontani
PRO
1
1.2k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
4
570
Git: the NoSQL Database
bkeepers
PRO
432
67k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
570
Leo the Paperboy
mayatellez
8
2.2k
Building the Perfect Custom Keyboard
takai
2
860
Docker and Python
trallard
47
4.2k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
430
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
490
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
YesSQL, Process and Tooling at Scale
rocio
174
15k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
720
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パターンを覚えよう