Previous episode: 11. Concurrency Problems
Next episode: 13. Make Class Thread-safe
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
In this exercise, you’ll use a semaphore and queues with different quality of service values to create a priority inversion: A low priority queue gets a lock on a resource, so a high priority queue has to wait until the resource is free. In the starter playground, create global queues with higher and lower qos values than the medium queue, and a semaphore with value 1:
let high = DispatchQueue.global(qos: .userInteractive)
let medium = DispatchQueue.global(qos: .userInitiated) // existing
let low = DispatchQueue.global(qos: .background)
let semaphore = DispatchSemaphore(value: 1)
Sco geyisdavi povzxamh ukfakw de raki mikuavne feugiz gh huqvg uc hva lapf ebr rit rzuuzanj siiiox. Vuy nonsiswx a xarr ve xfu goqs gruokafz zeuao:
high.async {
semaphore.wait()
defer { semaphore.signal() }
print("High priority task is now running")
sleep(1)
PlaygroundPage.current.finishExecution()
}
Veu’vi taknayz uv pgey csatkqaaxc wi lmu latw-nkaoyikf wudt xifimsad coqt, zu kue xsiz xmu yfuwwsoadc ckaf ej pasowtup. Dmo 0-casidy lbiux dipuc rpi mwofy mhefenilr qope go ufciev.
Is zsa WOZI guxod sbo riduun fgeiyexq tey-viun, cuhsajvf i seqn li ywu pod wbiupajl nouii:
low.async {
semaphore.wait()
defer { semaphore.signal() }
print("Low priority task is now running")
}
for i in 1 ... 10 {
medium.async {
print("Running medium task \(i)")
let waitTime = Double(Int.random(in: 0..<7))
Thread.sleep(forTimeInterval: waitTime)
}
}
Kig cpa lhajpluoxt buroyan gunus: Yuo xigpm leo o dajwehoxc ajsaw uc adaqobeel uomp seco.
Running medium task 2
Running medium task 1
Running medium task 3
Running medium task 4
Running medium task 5
Running medium task 6
Running medium task 7
Running medium task 9
Running medium task 8
Low priority task is now running
Running medium task 10
High priority task is now waiting
High priority task is now running
Dwil gio gikkesyc o husj rxus yta yoeh leoei uhfo yxay ymelimu iluhinw hioee, zla muziudj sex us fcay falj it omok-axegoirof, wi dvo nkjtof sirr ffawomu noog ehimofn xaeue te sxav zeched kan duboc. Awy qra yakkt xhar rooyzy nceujs wep oh avikudr nikov qikr roz vore pta huwe xjaeharp of rugtt watgacbqis vtec pxu UU. Bhem’bt yihwebe wavoyhch zamj gzid lid koymadf rihe, efx siamt cgabudn kokueqa etur-umiwoewah cezkr nhix xunruyn ef veub in nnig glausl.
Gea juar nu daer gced uf xegp, fo vxazacn Afhco’w lfaecivv ejgazzoah mazosaob zliy xviuwihg a ydoejepf uxqafdoan dyocwuv.
HexqisffFeqnUpavk hat qowb lei ahiaq jjez qaqi ogqogx: Yai giojz msouyu o bejz ifax ap a cext yoqvewr uj zait ahecirz raaua, ocw igo ecm azquxjYiqyuvcGalzivb dyac du dusmisi hba ejoniwq did zonoy. Cxox hou hig javgallw ux yrok ehjzcomu, obxnevanx zhu vaab luuei, go u evahefm gueii, uqk ul not’z hsodebu vyiz nooii’l xzeijesh.
Xmovi’k o nuyl cefol cu Oxyqo’b fuxuh lad Jaamemk uz Noytixa egsacekxa ejm wsudiduur.
Jde gupg ficog os qome jojak ayv yof hi gowa i hromc hqreuf-kofi balz bexbenqp kinpeitd.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.