Hello audrey It looks fine but If I am using the same mechanism in the Person class It is not giving me the expected result. Please find the code below.
open class Person {
private var firstName: String
private var lastName: String
let queue = DispatchQueue(label: "com.raywenderlich.person.isolation", attributes: .concurrent)
Current threadsafe name: Charlie Cheesecake
Current threadsafe name: Delia Dingle
Current threadsafe name: Freddie Frost
Current threadsafe name: Freddie Frost
Current threadsafe name: Gina Gregory
Final threadsafe name: Gina Gregory
As long as the first and last name start with the same letter, it’s working
hi audrey Thanks for the answer but one thing I have observed It is giving expected result only when we added this line. usleep(UInt32(10_000 * idx)) I didn’t get that.can we make class thread safe with out adding this line?
hi Rakesh: that line is in the for-loop to change the names, not in the Person class. It’s there to slow down execution, so the race condition appears. But it’s not actually necessary: I can get the jumbled names without it, because of the randomDelay in the changeName method.
For the thread safe Person, I get the correct results even if I comment out the usleep line.
Hello Mam, when I run the program normally I just got the expected result given below:
Current threadsafe name: Charlie Cheesecake
Current threadsafe name: Delia Dingle
Current threadsafe name: Eva Evershed
Current threadsafe name: Freddie Frost
Current threadsafe name: Gina Gregory
Final threadsafe name: Gina Gregory
but If I am commenting Line usleep(UInt32(10_000 * idx)) the I am getting results given below:
Current threadsafe name: Gina Gregory
Current threadsafe name: Gina Gregory
Current threadsafe name: Gina Gregory
Current threadsafe name: Gina Gregory
Current threadsafe name: Gina Gregory
Final threadsafe name: Gina Gregory
that is something which is confusing me Please help
your computer is so fast, it has already reached the last name change before it started printing, so you only get the last name printed, for each loop iteration.
my MacBook must be slower than yours … it’s a few years old
what I said wasn’t about barrier, but about sync — sorry, should’ve been more clear about that. You would use sync on an internal queue to ensure you initialised a lazy var at most once. Previously, you would use dispatch_once, but that’s gone away now.
hi Zac: I would do it only if necessary, that is, if your app changes objects of that class from concurrent threads. Or if TSan finds a race condition. Here’s an endorsement of TSan from a dev I respect:
Attaching a Swift 5 Xcode 11 version of exercise files for this lecture, could probably help those who don’t have Xcode 10 to be able to run the demo. Did nothing but used the default migration tool, and can verify that the end product works just fine (albeit with 2-3 warnings)
Won’t mind if this is made downloadable directly from the lecture video page - 11_ConcurrencySolutions.zip (1.8 MB)