Last active
March 31, 2023 07:47
-
-
Save vladimirgoncharov/c9604b2250066cf77e013b2edbf7f9af to your computer and use it in GitHub Desktop.
Using semaphore 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
import UIKit | |
/// ------------------------------------------------------------------- | |
private func example1() { | |
let queue = DispatchQueue.global(qos: DispatchQoS.QoSClass.default) | |
for i in 0..<10 { | |
queue.async() { | |
print("Start: \(i)") | |
sleep(3) | |
print("End: \(i)") | |
} | |
} | |
} | |
/** Logs: | |
Start: 0 | |
Start: 1 | |
Start: 2 | |
Start: 3 | |
Start: 4 | |
Start: 5 | |
Start: 6 | |
Start: 7 | |
Start: 8 | |
Start: 9 | |
End: 4 | |
End: 8 | |
End: 2 | |
End: 5 | |
End: 1 | |
End: 6 | |
End: 7 | |
End: 3 | |
End: 0 | |
End: 9 | |
*/ | |
/// ------------------------------------------------------------------- | |
private func semaphoreExample1() { | |
let semaphore = DispatchSemaphore(value: 3) | |
let queue = DispatchQueue.global(qos: DispatchQoS.QoSClass.default) | |
for i in 0..<10 { | |
queue.async() { | |
semaphore.wait(timeout: DispatchTime.distantFuture) | |
print("Start: \(i)") | |
sleep(3) | |
print("End: \(i)") | |
semaphore.signal() | |
} | |
} | |
} | |
/** Logs: | |
Start: 2 | |
Start: 1 | |
Start: 0 | |
End: 2 | |
End: 1 | |
End: 0 | |
Start: 4 | |
Start: 5 | |
Start: 3 | |
End: 4 | |
End: 5 | |
End: 3 | |
Start: 6 | |
Start: 7 | |
Start: 8 | |
End: 6 | |
End: 7 | |
End: 8 | |
Start: 9 | |
End: 9 | |
*/ | |
/// ------------------------------------------------------------------- | |
private func semaphoreExample2() { | |
let semaphore = DispatchSemaphore(value: 0) | |
let queue = DispatchQueue.global(qos: DispatchQoS.QoSClass.default) | |
queue.async() { | |
print("Running async task...") | |
sleep(3) | |
print("Async task completed") | |
semaphore.signal() | |
} | |
print("Waiting async task...") | |
semaphore.wait(timeout: DispatchTime.distantFuture) | |
print("Continue!") | |
} | |
/** Logs: | |
Running async task... | |
Waiting async task... | |
Async task completed | |
Continue! | |
*/ | |
/// ------------------------------------------------------------------- | |
private func semaphoreExample3() { | |
let semaphore = DispatchSemaphore(value: 0) | |
let queue = DispatchQueue.global(qos: DispatchQoS.QoSClass.default) | |
let n = 9 | |
for i in 0..<n { | |
queue.async() { | |
print("\(i): Running async task...") | |
sleep(3) | |
print("\(i): Async task completed") | |
semaphore.signal() | |
} | |
} | |
print("Waiting async task...") | |
for i in 0..<n { | |
print("Start waiting \(i)") | |
semaphore.wait(timeout: DispatchTime.distantFuture) | |
print("\(i + 1)/\(n) completed") | |
} | |
print("Continue!") | |
} | |
/** Logs: | |
0: Running async task... | |
1: Running async task... | |
2: Running async task... | |
3: Running async task... | |
4: Running async task... | |
5: Running async task... | |
6: Running async task... | |
7: Running async task... | |
8: Running async task... | |
Waiting async task... | |
Start waiting 0 | |
2: Async task completed | |
1: Async task completed | |
6: Async task completed | |
5: Async task completed | |
4: Async task completed | |
0: Async task completed | |
3: Async task completed | |
1/9 completed | |
Start waiting 1 | |
2/9 completed | |
Start waiting 2 | |
3/9 completed | |
Start waiting 3 | |
4/9 completed | |
Start waiting 4 | |
5/9 completed | |
Start waiting 5 | |
6/9 completed | |
Start waiting 6 | |
7/9 completed | |
Start waiting 7 | |
8: Async task completed | |
7: Async task completed | |
8/9 completed | |
Start waiting 8 | |
9/9 completed | |
Continue! | |
*/ | |
/// ------------------------------------------------------------------- | |
private func semaphoreExample4() { | |
let queue = DispatchQueue.global(qos: DispatchQoS.QoSClass.default) | |
let group = DispatchGroup() | |
let n = 9 | |
for i in 0..<n { | |
queue.async(group: group, execute: DispatchWorkItem.init(block: { | |
print("\(i): Running async task...") | |
sleep(3) | |
print("\(i): Async task completed") | |
})) | |
} | |
print("Waiting async task...") | |
group.wait(timeout: DispatchTime.distantFuture) | |
print("Continue!") | |
} | |
/** Logs: | |
0: Running async task... | |
1: Running async task... | |
2: Running async task... | |
3: Running async task... | |
4: Running async task... | |
5: Running async task... | |
6: Running async task... | |
7: Running async task... | |
Waiting async task... | |
8: Running async task... | |
8: Async task completed | |
6: Async task completed | |
3: Async task completed | |
5: Async task completed | |
0: Async task completed | |
4: Async task completed | |
1: Async task completed | |
7: Async task completed | |
2: Async task completed | |
Continue! | |
*/ | |
/// ------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment