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
150
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
590
Generate a rust client code by OpenAPI Generator
matsu7874
0
340
ざっと理解するRust 2024 Edition
matsu7874
0
880
プリントデバッグを失敗させないテクニック
matsu7874
1
320
社外を巻き込んだ勉強会を定期開催するコツ
matsu7874
0
170
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.2k
Other Decks in Programming
See All in Programming
2,500万ユーザーを支えるSREチームの6年間のスクラムのカイゼン
honmarkhunt
6
5.3k
Conform を推す - Advocating for Conform
mizoguchicoji
3
690
さいきょうのレイヤードアーキテクチャについて考えてみた
yahiru
3
750
sappoRo.R #12 初心者セッション
kosugitti
0
250
Kubernetes History Inspector(KHI)を触ってみた
bells17
0
230
『GO』アプリ バックエンドサーバのコスト削減
mot_techtalk
0
140
Amazon Bedrock Multi Agentsを試してきた
tm2
1
290
Amazon ECS とマイクロサービスから考えるシステム構成
hiyanger
2
560
DROBEの生成AI活用事例 with AWS
ippey
0
130
『テスト書いた方が開発が早いじゃん』を解き明かす #phpcon_nagoya
o0h
PRO
2
260
仕様変更に耐えるための"今の"DRY原則を考える / Rethinking the "Don't repeat yourself" for resilience to specification changes
mkmk884
0
220
Ruby on cygwin 2025-02
fd0
0
150
Featured
See All Featured
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Docker and Python
trallard
44
3.3k
Why Our Code Smells
bkeepers
PRO
336
57k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
366
25k
Why You Should Never Use an ORM
jnunemaker
PRO
55
9.2k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.1k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Build The Right Thing And Hit Your Dates
maggiecrowley
34
2.5k
For a Future-Friendly Web
brad_frost
176
9.5k
Code Reviewing Like a Champion
maltzj
521
39k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
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パターンを覚えよう