<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">You can use the GCD semaphore. <a href="https://developer.apple.com/reference/dispatch/dispatchsemaphore">https://developer.apple.com/reference/dispatch/dispatchsemaphore</a></div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Or it is always better to consider of using queues if you can. </div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif"><a href="https://developer.apple.com/library/content/documentation/General/Conceptual/ConcurrencyProgrammingGuide/ThreadMigration/ThreadMigration.html#//apple_ref/doc/uid/TP40008091-CH105-SW1">https://developer.apple.com/library/content/documentation/General/Conceptual/ConcurrencyProgrammingGuide/ThreadMigration/ThreadMigration.html#//apple_ref/doc/uid/TP40008091-CH105-SW1</a><br></div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Zhaoxin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 18, 2017 at 5:53 PM, Dale Myers 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">Compiler optimisations are something I completely ignored in my reasoning, which as has been pointed out was clearly a mistake.<br>
<br>
We will switch to using something that provides atomic operations.<br>
<br>
Thanks!<br>
<div class="HOEnZb"><div class="h5"><br>
-----Original Message-----<br>
From: <a href="mailto:johannesweiss@apple.com">johannesweiss@apple.com</a> [mailto:<a href="mailto:johannesweiss@apple.com">johannesweiss@apple.<wbr>com</a>]<br>
Sent: 17 January 2017 18:37<br>
To: Dale Myers <<a href="mailto:dalemy@microsoft.com">dalemy@microsoft.com</a>><br>
Cc: <a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
Subject: Re: [swift-users] Bool type and reading and writing operation atomicity<br>
<br>
Hi,<br>
<br>
> Put simply, are reading and writing to Bools both atomic operations in Swift (3)?<br>
<br>
you don't get a guarantee for that as Swift at the moment lacks both, a memory and a concurrency model.<br>
<br>
<br>
> Obviously, something reading and then assigning assuming the value doesn't change between is not correct, but would something like the following be fine:<br>
><br>
> var myBool = false<br>
><br>
> // Thread 1<br>
> while true {<br>
> if randomEvent() {<br>
> myBool = true<br>
> }<br>
> }<br>
><br>
> // Thread 2<br>
> while true {<br>
> if myBool {<br>
> print("Was set to true")<br>
> } else {<br>
> print("Not set")<br>
> }<br>
> }<br>
><br>
><br>
> I understand that in thread 2, the value of myBool can change between the check and the print statements, but that's fine. What I want to confirm for my team is that `myBool` can never be in some weird "third" state?<br>
<br>
As far as I know, you don't get a guarantee for that. But worse (and just like in C/C++/...) you most importantly don't get the guarantee that Thread 2 will ever see the change of Thread 1. The compiler could also optimise the code in Thread 2 to<br>
<br>
while true {<br>
// if false {<br>
// print("Was set to true")<br>
// } else {<br>
print("Not set")<br>
// }<br>
}<br>
<br>
ie. the compiler could 'prove' that `myBool` is never written (legally) and therefore delete all the code that assumes myBool != false.<br>
<br>
I'm not saying the compiler is doing this today but AFAIK it could do so if it wanted.<br>
<br>
<br>
> From what I can tell, behind the scenes, Bool uses a Builtin.Int1 for storage. I can't find the definition for this type anywhere so I can't check what it is really doing. My assumption is that it uses something like a C++ unsigned char behind the scenes, which would be fine with the above code on x86 and ARM as far as I'm aware.<br>
<br>
In C/C++ you'd need the new _Atomic variables to get that guarantee.<br>
<br>
<br>
> [...]<br>
<br>
Again, I'm not saying the compiler does do that but AFAIK it could do so legally. But one of the compiler people can answer these questions much better than I can.<br>
<br>
Cheers,<br>
Johannes<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>
</div></div></blockquote></div><br></div>