[swift-evolution] [Concurrency] async/await + actors

Thomas tclementdev at free.fr
Fri Aug 18 10:32:18 CDT 2017


The compiler could rewrite this:

print(await dataModel.getNumberOfEntries())

actor func getNumberOfEntries() -> Int
{
    return theList.count
}

as this:

dataModel.getNumberOfEntries(_internalQueue) { count in
    print(count)
}

actor func getNumberOfEntries(queue: DispatchQueue, handler: Int -> Void) -> Void
{
    _internalQueue.async {
        let count = theList.count
        queue.async {
            handler(count)
        }
    }
}

There is another problem that bothers me, if the function were to await on another actor (and therefore dispatch away from its _internalQueue), how would you guarantee that "only one message is processed by the actor at a time". You would need to somehow prevent the internal queue from processing other messages.

Thomas

> On 18 Aug 2017, at 17:13, Johannes Weiß via swift-evolution <swift-evolution at swift.org> wrote:
> 
> GCD doesn't actually allow you to dispatch back to the original queue, so I find it unclear how you'd achieve that. IMHO the main reason is that conceptually at a given time you can be on more than one queue (nested q.sync{}/target queues). So which is 'the' current queue?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170818/df09b87b/attachment.html>


More information about the swift-evolution mailing list