Ok so I have gone through a ton of questions and answers and I know the understanding of it, but when I am trying some code, I am getting some results that doesnt stand by those theories.
What I understand till now :
- Sync : Control will return once all tasks inside the block will be executed.
- Async : Control will return immediately after the block is hit.
This is the part that seems fine. Now comes the tricky part
- Serial : Tasks will be performed in a serial way inside the block. Q1 : does this apply to tasks inside the block? as it is already happening in all scenario. Also for sync serial queues, adding tasks in different block does not matter, as control will return once the first task is done.
Eg-
let syncQ = DispatchQueue(label:"xyz") // by default it is serial
syncQ.sync{
for _ in 0...10{
print("ABC")
}
}
syncQ.sync{
for _ in 0...10{
print("XYZ")
}
}
Expected Output : ABC * 10 , XYZ * 10 This is fine.
Now when I introduce concurrent serial Q, the output is same. So my question is as concurrent queues say that tasks will be done in the same time or concurrently, it isnt happening.
Eg -
let syncConc = DispatchQueue(label:"con",attributes:.concurrent)
syncConc.sync{
for _ in 0...10{
print("XYZ")
}
for _ in 0...10{
print("ABC")
}
}
syncConc.sync{
for _ in 0...10{
print("HHH")
}
for _ in 0...10{
print("XXX")
}
}
Output : XYZ *10 ,ABC*10, HHH*10, XXX*10
So it seems that Synchronous Concurrent Queue, act like serial Queues, and only way to make concurrent operations is that if we throw a Asynchronous queue in between the action. So from this I cannot understand, what is the purpose of concurrent type of serial queues.
If anyone can give coded examples, it will be much appreciated, as I already know the theory and working of it. Much appreciated.