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
360
わいわい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
Into the Great Unknown - MozCon
thekraken
41
2.7k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
2
2.9k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
240
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
203
76k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
3k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
600
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
57k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
510
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
Everyday Curiosity
cassininazir
0
320
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)を使っていこう