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
110
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
社外を巻き込んだ勉強会を定期開催するコツ
matsu7874
0
140
actix-webを使った開発のハマリポイントを避けたい
matsu7874
0
820
our test strategy on actix-web app
matsu7874
0
1.2k
roadmap to rust 2024
matsu7874
0
1.7k
Rust tutorial for Pythonista
matsu7874
2
970
Mock testing with mockall
matsu7874
0
250
Write Postgres Extensions with Rust!
matsu7874
0
550
Self-learning Rust way with Clippy.
matsu7874
0
670
Property-Based Testing in Rust
matsu7874
0
900
Other Decks in Programming
See All in Programming
SwiftUIで使いやすいToastの作り方 / How to build a Toast system which is easy to use in SwiftUI
lovee
3
130
Netty Chicago Java User Group 2024-04-17
sullis
0
170
雑に思考を整理する技術と効能
konifar
58
29k
GitHub Copilotのススメ
marcy731
1
190
CA.swift19 恋するAIアプリ開発の裏側
oskmr
0
360
はてなにおける CSS Modules、及び CSS Modules に足りないもの / CSS Modules in Hatena, and CSS Modules missing parts
mizdra
6
910
Hanami and htmx
bkuhlmann
0
200
Compose-View Interop in Practice (mDevCamp 2024)
stewemetal
0
120
Rethinking UI building strategies @ SFI 2024
letelete
0
270
Folding Cheat Sheet #3
philipschwarz
PRO
0
120
Amazon SQSコンシューマー疎結合への旅 - 出張! #DevelopersIO IT技術ブログの中の人が語る勉強会 #3
quiver
0
230
Prepare for Jakarta EE 11 - Performance and Developer Productivity
ivargrimstad
0
700
Featured
See All Featured
Into the Great Unknown - MozCon
thekraken
10
990
How to train your dragon (web standard)
notwaldorf
73
5.2k
[RailsConf 2023] Rails as a piece of cake
palkan
23
3.9k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
Building Your Own Lightsaber
phodgson
99
5.7k
How to name files
jennybc
65
93k
Learning to Love Humans: Emotional Interface Design
aarron
267
39k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
17
1.4k
Six Lessons from altMBA
skipperchong
21
3k
Making Projects Easy
brettharned
108
5.5k
What's in a price? How to price your products and services
michaelherold
237
11k
Code Reviewing Like a Champion
maltzj
514
39k
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パターンを覚えよう