Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
DispatchQueue.syncが動作するスレッド
Search
Iceman
October 29, 2018
400
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
DispatchQueue.syncが動作するスレッド
Iceman
October 29, 2018
More Decks by Iceman
See All by Iceman
わいわいswift#39 Swiftの型をTypeScriptで表す
sidepelican
0
380
わいわいswiftc#35夢が広がる!コード生成でどこでもSwift
sidepelican
0
500
元ゲーム開発者が贈る描画パフォーマンス改善 / Rendering performance improvement from a game developer
sidepelican
4
1.9k
わいわいswiftc#19Genericsの特殊化
sidepelican
0
500
わいわいswiftc#17Genericsの特殊化
sidepelican
0
110
SwiftUI: 更新検知と値の生存期間
sidepelican
2
1.2k
クックパッドiOSアプリのパフォーマンス改善
sidepelican
0
830
Featured
See All Featured
Marketing to machines
jonoalderson
1
6k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.7k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
22k
Believing is Seeing
oripsolob
1
260
Chasing Engaging Ingredients in Design
codingconduct
0
380
Site-Speed That Sticks
csswizardry
13
1.6k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.9k
BBQ
matthewcrist
90
10k
Making Projects Easy
brettharned
120
6.8k
Side Projects
sachag
456
43k
Un-Boring Meetings
codingconduct
0
480
Transcript
DispatchQueue.sync が動作するスレッド
自己紹介 Twitter: Iceman (@iceman5499) Github:okamura (sidepelican) クックパッド株式会社 名前とアイコンとIDがブレブレ太郎なので最近はIcemanで統一しよ うと考えてます
DispatchQueueでありがちなデッドロック 軽い気持ちで呼んだ関数が内部で DispatchQueue.sync を使っていた
便利関数が用意される
これでクラッシュしない! めでたしめでたし
しかしここで疑問が 次のようなコードを実行するとき
None
しかしここで疑問が 実際に実行するとクラッシュしない この場合 Thread.isMainThread は true を返す myqueue の中から呼んでいるのに、なぜ? (この時点で僕は勘違いをしています)
apple/swift‑corelibs‑libdispatch
デッドロックを発生させてエラーからソースをたどる dispatch_sync called on queue already owned by current thread
というエラー文をswift‑corelibs‑libdispatchから grep → 出ない
あった( _dispatch_sync_wait ) C++の文字列リテラルは空白や改行があれば結合されるので、そこ で分かれていた
デッドロック判定処理をたどる _dq_state_drain_locked_by が判定しているっぽい typedef uint32_t dispatch_tid; typedef uint32_t dispatch_lock;
#define DLOCK_OWNER_MASK ((dispatch_lock)0xfffffffc) C にありがちなビット演算 dq_state と tid の下位3~32bitが同一かどうかで比較されてる dq_state
は DispatchQueue が管理している値っぽいので、 tid が何か分かれば良さそう
_dispatch_sync_waitに戻る tid は _dispatch_tid_self() から来てるっぽい
#define _dispatch_tid_self() \ ((dispatch_tid)_dispatch_thread_port()) #define _dispatch_thread_port() \ pthread_mach_thread_np(_dispatch_thread_self()) ようやく見慣れた関数が出てくる (
pthread_self ) OSによって分岐しているが、実態はpthreadのスレッドID(あるい は同等にみなせる代物)っぽい
_dispatch_sync_waitに戻る sync処理を行う対象のスレッドが現在のスレッドならデッドロック と判定しクラッシュさせている
最初の疑問に戻り、なぜrunOnMainThreadは問題なさ そうなのか
None
そもそもの勘違い DispatchQueue はそれぞれが専用のスレッドを持っていてその中 でのみ動作するものだと思いこんでいた これだとconcurrent queueは何者やねん!ってなる
最後に 事の発端となった DispatchQueue.main.sync だが、そもそもこ れをしなきゃいけない場面はほぼないはず 適切に DispatchQueue (や、Lock)を使っていこう