Last active
April 27, 2021 06:17
-
-
Save VAndrJ/7d14d91d03480a3b4f026c8d7aa36d8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// StackTest | |
// | |
// Created by Volodymyr Andriienko on 27.04.2021. | |
// | |
// Что здесь происходит: | |
// r - отправка на чтение (первый таймер). | |
// w - отправка на запись (второй таймер). | |
// == - выполнение чтения. | |
// ++ - выполнение записи. | |
// | |
// r r r rw r r r r r r r r r r r r r r | |
//-====--====--====--====--====--====--====--====--====-- | |
//----====--====--====--====--====--====--====--====--=== | |
//-----------++++++++++++-------------------------------- | |
// 👆вот здесь и возникает проблема, так как: | |
// первое - уже некорректность данных | |
// второе - в случае потоконебезопасных операций чтения/записи будет ой-ой | |
// | |
import UIKit | |
let concurrentQueue = DispatchQueue(label: "concurrentQueue", attributes: .concurrent) | |
let instance = Person() | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Timer точнее asyncAfter :) | |
Timer.scheduledTimer(withTimeInterval: 0.75, repeats: true, block: { (timer) in | |
concurrentQueue.async { | |
print(timer.fireDate, Date(), instance.name) | |
} | |
}) | |
Timer.scheduledTimer(withTimeInterval: 6, repeats: true, block: { (timer) in | |
concurrentQueue.async { | |
instance.name = "name\(Int.random(in: 0...10000))" | |
} | |
}) | |
} | |
} | |
class Person { | |
private var _name = "name" | |
// Добавляем задержек для наглядности. | |
var name: String { | |
get { | |
sleep(1) | |
return _name | |
} | |
set { | |
sleep(3) | |
_name = newValue | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// StackTest | |
// | |
// Created by Volodymyr Andriienko on 27.04.2021. | |
// | |
// Что здесь происходит: | |
// r - отправка на чтение (первый таймер). | |
// w - отправка на запись (второй таймер). | |
// == - выполнение чтения. | |
// ++ - выполнение записи. | |
// | |
// r r r rw r r r r r r r r r r r r r r | |
//-====--====---------------====-====--====--====--====-- | |
//----====--====------------====----====--====--====--=== | |
//--------------------------====------------------------- | |
//--------------------------====------------------------- | |
//--------------------------====------------------------- | |
//----------------------------====----------------------- | |
//--------------++++++++++++----------------------------- | |
// 👆Вот здесь приходит важный barrier: | |
// так, кто последний из выполняющихся? За тобой буду. | |
// А каждой новой операции: за мной будешь, как только закончу - так сразу. | |
// | |
import UIKit | |
let concurrentQueue = DispatchQueue(label: "concurrentQueue", attributes: .concurrent) | |
let instance = Person() | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
Timer.scheduledTimer(withTimeInterval: 0.75, repeats: true, block: { (timer) in | |
concurrentQueue.async { | |
print(timer.fireDate, Date(), instance.name) | |
} | |
}) | |
Timer.scheduledTimer(withTimeInterval: 6, repeats: true, block: { (timer) in | |
concurrentQueue.async { | |
instance.name = "name\(Int.random(in: 0...10000))" | |
} | |
}) | |
} | |
} | |
class Person { | |
private var _name = "name" | |
private let queue = DispatchQueue(label: "personQueue", attributes: .concurrent) | |
var name: String { | |
get { | |
queue.sync { | |
sleep(1) | |
return _name | |
} | |
} | |
set { | |
queue.async(flags: .barrier) { [weak self] in | |
sleep(3) | |
self?._name = newValue | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment