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
180
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
6.9k
Marpを使って登壇資料を作る
matsu7874
0
1.4k
Generate a rust client code by OpenAPI Generator
matsu7874
0
520
ざっと理解するRust 2024 Edition
matsu7874
0
1.5k
プリントデバッグを失敗させないテクニック
matsu7874
1
400
社外を巻き込んだ勉強会を定期開催するコツ
matsu7874
0
200
actix-webを使った開発のハマリポイントを避けたい
matsu7874
0
1.1k
our test strategy on actix-web app
matsu7874
0
1.6k
roadmap to rust 2024
matsu7874
0
2.1k
Other Decks in Programming
See All in Programming
print("Hello, World")
eddie
2
530
Updates on MLS on Ruby (and maybe more)
sylph01
1
180
AIと私たちの学習の変化を考える - Claude Codeの学習モードを例に
azukiazusa1
9
3.7k
Introducing ReActionView: A new ActionView-compatible ERB Engine @ Rails World 2025, Amsterdam
marcoroth
0
670
知っているようで知らない"rails new"の世界 / The World of "rails new" You Think You Know but Don't
luccafort
PRO
1
110
基礎から学ぶ大画面対応(Learning Large-Screen Support from the Ground Up)
tomoya0x00
0
420
プロポーザル駆動学習 / Proposal-Driven Learning
mackey0225
2
1.2k
さようなら Date。 ようこそTemporal! 3年間先行利用して得られた知見の共有
8beeeaaat
3
1.4k
為你自己學 Python - 冷知識篇
eddie
1
350
意外と簡単!?フロントエンドでパスキー認証を実現する WebAuthn
teamlab
PRO
2
730
旅行プランAIエージェント開発の裏側
ippo012
2
890
Azure SRE Agentで運用は楽になるのか?
kkamegawa
0
2.1k
Featured
See All Featured
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Reflections from 52 weeks, 52 projects
jeffersonlam
352
21k
It's Worth the Effort
3n
187
28k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.5k
Building Adaptive Systems
keathley
43
2.7k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Art, The Web, and Tiny UX
lynnandtonic
302
21k
Testing 201, or: Great Expectations
jmmastey
45
7.7k
The Language of Interfaces
destraynor
161
25k
Become a Pro
speakerdeck
PRO
29
5.5k
Into the Great Unknown - MozCon
thekraken
40
2k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.9k
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パターンを覚えよう