Skip to content

Instantly share code, notes, and snippets.

@lauramarson
lauramarson / ATMThreadSafe.swift
Created July 26, 2023 18:13
Playground do XCode para testar um ATM threadsafe
import Foundation
func randomDelay(maxDuration: Double) {
let randomWait = UInt32.random(in: 0..<UInt32(maxDuration * Double(USEC_PER_SEC)))
usleep(randomWait)
}
let isolationQueue = DispatchQueue(label: "com.myproject.atm.isolation",
attributes: .concurrent)
@lauramarson
lauramarson / Bank.swift
Last active July 26, 2023 18:13
ATM Threadsafe
//
// Bank.swift
//
// Created by Laura Pinheiro Marson on 25/07/23.
//
import Foundation
func randomDelay(maxDuration: Double) {
let randomWait = UInt32.random(in: 0..<UInt32(maxDuration * Double(USEC_PER_SEC)))
@lauramarson
lauramarson / DataRaceExample.swift
Created July 22, 2023 14:28
Playground do Xcode para testar um problema de Data Race
import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let concurrentQueue = DispatchQueue.global()
var counter = 0
concurrentQueue.async {
for n in 0..<100 {
@lauramarson
lauramarson / ConcurrencyTest.swift
Last active June 2, 2023 18:02
Playground do Xcode para testar concorrência no iOS
import UIKit
import PlaygroundSupport
///Habilite a execução indefinida do playground para evitar que as tasks executadas em background sejam interrompidas quando a main thread for finalizada.
PlaygroundPage.current.needsIndefiniteExecution = true
///Criando uma private queue concorrente
let concurrentQueueA = DispatchQueue(label: "com.myproject.concurrent",
qos: .utility,
attributes: .concurrent)