Last active
July 18, 2025 14:09
-
-
Save soxjke/4641cd433b254f8d5b6911f995ca744f to your computer and use it in GitHub Desktop.
Concurrency examples in swift
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
| class Counter { | |
| var value = 0 | |
| func increment() { | |
| value += 1 | |
| } | |
| } | |
| let counter = Counter() | |
| let queue = DispatchQueue.global(qos: .userInitiated) | |
| for _ in 0..<1000 { | |
| queue.async { | |
| counter.increment() | |
| } | |
| } |
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
| let serialQueue = DispatchQueue(label: "com.example.serialQueue") | |
| func fetchData(completion: @escaping () -> Void) { | |
| serialQueue.async { | |
| print("Fetching data on background serial queue") | |
| // Some network fetch... | |
| sleep(1) | |
| DispatchQueue.main.sync { | |
| print("Passing result back to the UI") | |
| completion() | |
| } | |
| } | |
| } | |
| // ... in ViewController | |
| func viewDidLoad() { | |
| super.viewDidLoad() | |
| print("View did load on main thread") | |
| serialQueue.sync { | |
| fetchData { | |
| print("Data fetched") | |
| } | |
| } | |
| } |
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
| let queue = DispatchQueue(label: "com.example.limitedQueue", attributes: .concurrent) | |
| let semaphore = DispatchSemaphore(value: 2) // Limit concurrency to 2 | |
| func doTask(id: Int) { | |
| semaphore.wait() | |
| print("Task \(id) started on thread: \(Thread.current)") | |
| let start = Date() | |
| while Date().timeIntervalSince(start) < 3 | |
| print("Task \(id) finished") | |
| semaphore.signal() | |
| } | |
| for i in 1...5 { | |
| queue.async { | |
| doTask(id: i) | |
| } | |
| } | |
| for i in 1...5 { | |
| queue.async { | |
| print("Just something async \(i) running on thread: \(Thread.current)") | |
| } | |
| } |
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
| import Foundation | |
| class LockedCounter { | |
| private var value = 0 | |
| private let lock = NSLock() | |
| func increment() { | |
| lock.lock() | |
| value += 1 | |
| lock.unlock() | |
| } | |
| func getValue() -> Int { | |
| lock.lock() | |
| let val = value | |
| lock.unlock() | |
| return val | |
| } | |
| } | |
| let counter = LockedCounter() | |
| let group = DispatchGroup() | |
| for _ in 0..<1000 { | |
| DispatchQueue.global().async(group: group) { | |
| counter.increment() | |
| } | |
| } | |
| group.wait() | |
| print("Final counter value with lock: \(counter.getValue())") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment