I forgot to reply, a shared value type can capture by multiple closures.

func twoThreads() -> (Thread, Thread) {
    var shared_int = 0
    return (Thread { shared_int = 1 }, Thread { shared_int = 2 })
}

Johannes Neubauer <[email protected]> 於 2016年7月18日星期一 寫道:

>
> > Am 18.07.2016 um 06:47 schrieb Susan Cheng <[email protected]
> <javascript:;>>:
> >
> > so, you want to propose default == operator but not forbidding all
> peoples to custom == operator?
> > Why don't just adding the following function to std library?
> >
> > public func == <T : Equatable>(lhs: T, rhs: T) -> Bool {
> >     var lhs = lhs
> >     var rhs = rhs
> >     return memcmp(&lhs, &rhs, sizeof(T.self)) == 0
> > }
>
> This does not work, because method parameters are statically dispatched.
> This function will never be executed for any type, that has a custom
> equality implementation. So this would not enforce this check upfront. You
> would need to copy this code to every custom implementation (which can be
> forgotten). Or you have to implement it like this
>
> ```swift
> public func isSame(lhs: Any, rhs: Any) -> Bool {
>   // like above in your code
> }
>
> public func ==(lhs: MyType, rhs: MyType) -> Bool {
>   if isSame(lhs, rhs: rhs) {
>     return true
>   }
>   // do custom behavior
> }
> ```
>
> > Thread safety can't fixed by std library. Value type only can atomic
> compared when additional mutex provided.
>
> Value types are either on the stack or they are **copied** to the heap
> (for protocol types with large value types). So, you don’t have any thread
> safety issues (as they are copied before they are changed in the new
> thread) as long as you don’t do (or check) anything on a property of a
> reference type, because the latter has shared **state**.
>
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to