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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
370
わいわいswiftc#35夢が広がる!コード生成でどこでもSwift
sidepelican
0
490
元ゲーム開発者が贈る描画パフォーマンス改善 / 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
820
Featured
See All Featured
Ten Tips & Tricks for a 🌱 transition
stuffmc
1
240
The SEO identity crisis: Don't let AI make you average
varn
0
560
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.5k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
530
For a Future-Friendly Web
brad_frost
183
10k
Code Review Best Practice
trishagee
74
20k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
From π to Pie charts
rasagy
1
380
A designer walks into a library…
pauljervisheath
211
25k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
69
66k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.8k
Automating Front-end Workflow
addyosmani
1369
210k
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)を使っていこう