Skip to content

Instantly share code, notes, and snippets.

@misbell
Created October 6, 2016 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misbell/bae00230c42e9ff48d53e609618eb4a3 to your computer and use it in GitHub Desktop.
Save misbell/bae00230c42e9ff48d53e609618eb4a3 to your computer and use it in GitHub Desktop.
DispatchWorkItem barrier attribute
so this works as expected:
let conq = DispatchQueue(label: "ok", qos: .background, attributes: .concurrent )
// let conq = DispatchQueue.global(qos: .userInitiated)
let dw1 = DispatchWorkItem {
//sleep(2)
print("dw1")
}
let dw2 = DispatchWorkItem {
//sleep(2)
print("dw2")
}
let dw3 = DispatchWorkItem {
//sleep(2)
print("dw3")
}
let dwbarrier = DispatchWorkItem(flags: .barrier) {
print("barrier")
}
let dw4 = DispatchWorkItem {
print("dw4")
}
let dw5 = DispatchWorkItem {
print("dw5")
}
let dw6 = DispatchWorkItem {
print("dw6")
}
conq.suspend()
conq.async(execute: dw1)
conq.async(execute: dw2)
conq.async(execute: dw3)
conq.async(execute: dwbarrier)
conq.async(execute: dw4)
conq.async(execute: dw5)
conq.async(execute: dw6)
conq.resume()
}
producing:
dw2
dw1
dw3
barrier
dw4
dw5
dw6
BUT if you change to the global concurrent queue
let conq = DispatchQueue.global(qos: .background)
producing:
dw6
barrier
dw5
dw4
dw2
dw3
dw1
barrier no longer works
this may be 'as designed' but should be so noted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment