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

Chris Lattner clattner at nondot.org
Mon Sep 11 23:00:50 CDT 2017


On Sep 4, 2017, at 12:18 PM, Pierre Habouzit <phabouzit at apple.com> wrote:
> Something else I realized, is that this code is fundamentally broken in swift:
> 
> actor func foo()
> {
>     NSLock *lock = NSLock();
>     lock.lock();
> 
>     let compute = await someCompute(); <--- this will really break `foo` in two pieces of code that can execute on two different physical threads.
>     lock.unlock();
> }
> 
> 
> The reason why it is broken is that mutexes (whether it's NSLock, pthread_mutex, os_unfair_lock) have to be unlocked from the same thread that took it. the await right in the middle here means that we can't guarantee it.

Agreed, this is just as broken as:

func foo()
{
    let lock = NSLock()
    lock.lock()

    someCompute {
	    lock.unlock()
    }
}

and it is just as broken as trying to do the same thing across queues.  Stuff like this, or the use of TLS, is just inherently broken, both with GCD and with any sensible model underlying actors.  Trying to fix this is not worth it IMO, it is better to be clear that they are different things and that (as a programmer) you should *expect* your tasks to run on multiple kernel threads.

BTW, why are you using a lock in a single threaded context in the first place??? ;-)

-Chris

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


More information about the swift-evolution mailing list