Slides accompanying the 360|iDev concurrency workshop from 2016
ConcurrencyworkshopSam Davies@iwantmyrealname_github.com/sammyd git.io/v6waD
View Slide
concurrencyis hard
what isconcurrency?
why useconcurrency?
commonconcurrencyproblems
race condition
Race ConditiontimeValue 1Thread 1Thread 2inc
Race ConditiontimeValue 1 1Thread 1Thread 2inc r1
Race ConditiontimeValue 1 1 1Thread 1Thread 2inc r1 +1
Race ConditiontimeValue 1 1 1 2Thread 1Thread 2inc r1 w2+1
Race ConditiontimeValue 1 1 1 2 2Thread 1Thread 2inc r1 w2+1inc
Race ConditiontimeValue 1 1 1 2 2 2Thread 1Thread 2inc r1 w2+1inc r2
Race ConditiontimeValue 1 1 1 2 2 2 2Thread 1Thread 2inc r1 w2+1inc r2 +1
Race ConditiontimeValue 1 1 1 2 2 2 2 3Thread 1Thread 2inc r1 w2+1inc r2 w3+1
Race ConditiontimeValue 1 1Thread 1Thread 2inc r1inc
Race ConditiontimeValue 1 1 1Thread 1Thread 2inc r1inc r1
Race ConditiontimeValue 1 1 1 1Thread 1Thread 2inc r1inc r1 +1
Race ConditiontimeValue 1 1 1 21Thread 1Thread 2inc r1inc r1 w2+1
Race ConditiontimeValue 1 1 1 21 2Thread 1Thread 2inc r1 +1inc r1 w2+1
Race ConditiontimeValue 1 1 1 21 2 2Thread 1Thread 2inc r1 w2+1inc r1 w2+1
priorityinversion
Priority Inversiontimelowpriorityresource
Priority Inversiontimelowmediumpriorityresource
Priority Inversiontimelowmediumhighpriorityresource
deadlock
Deadlocktimeresource1resource2Thread 1Thread 2
$&'(queueueueueue paradigm
Tasks & QueuesThread 1Thread 2Queue
feelingbrave?
a touch moretheory
Thread SafetytimeValue 1111Thread 1Thread 2Thread 3
Thread SafetytimeValue 1111Thread 1Thread 2r1111Thread 3
Thread SafetytimeValue 1111 1112Thread 1Thread 2r1111w2222Thread 3
Thread SafetytimeValue 1111 1112 1122Thread 1Thread 2r1111w2222r1122Thread 3
Thread SafetytimeValue 1111 1112 1122 1223Thread 1Thread 2r1111w2222r1122 w3333Thread 3 r1223
Thread SafetytimeValue 1111 1112 1122 1223 2233Thread 1Thread 2r1111w2222r1122 w3333Thread 3 r1223
Thread SafetytimeValue 1111 1112 1122 1223 2233 2333Thread 1Thread 2r1111w2222r1122r2333w3333Thread 3 r1223
Thread SafetytimeValue 1111 1112 1122 1223 2233 2333 3333Thread 1Thread 2r1111w2222r1122r2333w3333Thread 3 r1223
Thread SafetytimeValue 1111 1112 1122 1223 2233 2333 3333 3333 3333Thread 1Thread 2r1111w2222r1122r2333w3333Thread 3 r1223 r3333r3333
Dispatch Barriertimetasktasktasktasktaskbarrier task tasktasktasktask tasktasktask
Dispatch Barriertimereadreadreadreadreadreadwrite readreadreadread readread
alternativesto
promisesnetworkRequest(url, callback: { data indecompressor(data, callback: { image inimageResizer(image, callback: { small infilterImage(small, callback: { filtered indisplayImage(filtered)})})})})
promisespromiseint error
promisesstr
promisespromisestrf
promisespromisestrerrorfimg
promisespromisestrerrorfimgerror handler
promisespromisestrerrorfimggpromiseerror handler
promisespromisestrerrorfimggpromiseerrorerror handlerimg
promisesnetworkRequest(url).then(decompressor).then(imageResizer).then({ (image) inreturn filterImage(image)}).then(displayImage).catch({ (error) in print(error) })
imagine the“promise pipeline”accepted asequence of values…
that’s reactiveprogramming
conclusion
you will getit wrong
• raywenderlich.com/videos• git.io/v6waD• @iwantmyrealname