<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">You can use serial queue.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"><span style="color:rgb(186,45,162)">class</span> Car {</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"> <span style="color:rgb(186,45,162)">var</span> helper = <span style="color:rgb(79,129,135)">Helper</span>()</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"> <span style="color:rgb(186,45,162)">lazy</span> <span style="color:rgb(186,45,162)">private</span> <span style="color:rgb(186,45,162)">var</span> queue = <span style="color:rgb(112,61,170)">DispatchQueue</span>(label: <span style="color:rgb(209,47,27)">"my queue"</span>)</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0);min-height:13px"> </p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"> <span style="color:rgb(186,45,162)">func</span> test() {</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"> <span style="color:rgb(79,129,135)">helper</span>.<span style="color:rgb(49,89,93)">doSomething</span>(f1: <span style="color:rgb(49,89,93)">f1</span>)</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"> }</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0);min-height:13px"> </p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"> <span style="color:rgb(186,45,162)">func</span> f1() {</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"> <span style="color:rgb(79,129,135)">queue</span>.<span style="color:rgb(62,30,129)">async</span> {</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(186,45,162)">_</span><span style="color:rgb(0,0,0)"> = </span><span style="color:rgb(186,45,162)">self</span><span style="color:rgb(0,0,0)">.</span><span style="color:rgb(79,129,135)">helper</span><span style="color:rgb(0,0,0)">.</span><span style="color:rgb(79,129,135)">v1</span><span style="color:rgb(0,0,0)"> </span>//Crash - Simultaneous accesses to <memory address>, but modification requires exclusive access.</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"> }</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"> }</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)">}</p><p style="margin:0px;line-height:normal;color:rgb(0,0,0)"><font face="georgia, serif"><br></font></p><p style="margin:0px;line-height:normal"><font face="georgia, serif" color="#000000">Zhao Xin</font></p></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jul 24, 2017 at 4:22 PM, Quinn "The Eskimo!" via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
On 24 Jul 2017, at 07:04, somu subscribe via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br>
<br>
> - Is there a bug in my code which is being detected in Xcode 9 ?<br>
<br>
</span>Yes. The problem here is that `doSomething(f1:)` is a mutating function, so it acts like it takes an `inout` reference to `self.helper`. That’s one mutable reference. It then calls `Car.f1()`, which tries to get a non-mutating reference to exactly the same struct. This is outlawed in Swift 4 as part of the memory ownership effort.<br>
<br>
You can read more about the specific change in SE-0176 “Enforce Exclusive Access to Memory”.<br>
<br>
<<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0176-enforce-exclusive-access-to-memory.md" rel="noreferrer" target="_blank">https://github.com/apple/<wbr>swift-evolution/blob/master/<wbr>proposals/0176-enforce-<wbr>exclusive-access-to-memory.md</a>><br>
<br>
And the general background to this in the “Ownership Manifesto"<br>
<br>
<<a href="https://github.com/apple/swift/blob/master/docs/OwnershipManifesto.md" rel="noreferrer" target="_blank">https://github.com/apple/<wbr>swift/blob/master/docs/<wbr>OwnershipManifesto.md</a>><br>
<span class=""><br>
> If so could you please explain and suggest an alternate approach / fix ?<br>
<br>
</span>It’s hard to offer concrete suggestions without knowing more about your high-level goals. One option is for `doSomething(f1:)` to pass the `inout` reference through to `f1`. For example:<br>
<br>
mutating func doSomething(f1: (inout Helper) -> ()) {<br>
f1(&self)<br>
}<br>
<br>
func f1(h: inout Helper) {<br>
_ = h.v1 // no crash<br>
}<br>
<br>
but whether that makes sense in your code is for you to decide.<br>
<br>
Share and Enjoy<br>
--<br>
Quinn "The Eskimo!" <<a href="http://www.apple.com/developer/" rel="noreferrer" target="_blank">http://www.apple.com/<wbr>developer/</a>><br>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware<br>
<br>
<br>
______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
</blockquote></div><br></div>