And what is the thread doing when waiting for resources? How does the original function which needed the resource continue where it left off? We understand that we can write code that doesn't need async/await. After all, we've been doing it in Swift from day one. What we're not understanding is why you think we should continue to do so. If the only benefit was clearer and simpler code, it would be a clear win.
[edit]
To be clear, I think the benefit of going from A to B is sufficient reason to introduce async/await. Can you explain why we should refrain from doing so?
A
func longRunningJob(completion: @escaping (Result) -> ()) {
// do stuff
completion(result)
}
func useResultFromLongRunningJob() {
queue.async {
longRunningJob { result in
DispatchQueue.main.async {
// use result
}
}
}
B
func longRunningJob() async -> Result {
// do stuff
return result
}
func useResultFromLongRunningJob() {
let result = await longRunningJob()
// use result
}
Sorry if that wasn't clear. I was trying to say that event handlers tend to have problems composing with flow-controls, like if-else. If we're trying to have the same event handler across the if block, things become quite tricky:
// Work 1
if condition {
// Work 2a
await ...
// Work 2b
} else {
// Work 3
}
// Work 4
This is pretty tricky to achieve with traditional closure techniques.
No. First off, you're creating another thread to do the work, so your code is multi-threaded, not single threaded as requested. But whatever, that's not the most important bit.
The important part is that I think you might be misunderstanding how await works. Async/await is not a 'busy', active wait as you have, but a passive one. Your code is spinning in a while loop, actively polling for something to change, so as written, your thread is blocking, wasting cycles polling, and can do no further work, so there is no concurrency.
The async/await version is entirely different - at the await the thread can sleep, not wasting any cycles at all until the work completes. Or it could be 'awoken' and re-enter off the back of an event or something else and do more work, so we gain concurrency.
The whole point of await is that the thread does not sleep. Instead, it is free to run other code. The function which invoked await will be resumed when the value it is waiting for becomes available. This happens automagically, without any polling or context switches (unless a different thread is required, e.g. with actors).
Read it, it is written in the proposal. If you have a hard time understanding it that I will explain: a function that has gotten onto an await point stall until the awaited target has finished its work.
Let me emphasize it again:
It is written in the doc!
And as a little explanation, I say that:
parallelism = multithreading & no data dependencies between thread (they use TLS or something similar)
concurrency = multithreading & data dependencies between threads (they have to coordinate mutation of shared mutable data)
At this point, you are either scoff me or have no idea what you are talking about. I suggest you first try to work with something like posix threads and semaphores, or atomics and later come here to share your precious opinion.
Concurrency and multi-threading are orthogonal. This may be the point you are not understanding, and why you feel so much antipathy towards the proposed features.