Skip to content

Instantly share code, notes, and snippets.

View SysCall97's full-sized avatar

SysCall97 SysCall97

View GitHub Profile
import Foundation
// Create a custom concurrent queue
let concurrentQueue = DispatchQueue(label: "com.example.concurrentQueue", attributes:.concurrent)
var sharedArray: [Int] = []
let numberOfThreadsAllowedToAccessSharedReource: Int = 1
// Semaphore to control access the shared resource
import Foundation
// Create a custom concurrent queue
let concurrentQueue = DispatchQueue(label: "com.example.concurrentQueue", attributes:.concurrent)
var sharedArray: [Int] = []
// Function to write to the shared array in a thread-safe manner using barrier
func writeToArray(element: Int) {
concurrentQueue.async(flags: .barrier) {
import Foundation
class Search {
private var suggestedCitiesWorkItem: DispatchWorkItem?
func getSuggestedCities(for prefix: String) {
// here cancelling the previous workitem before assigning the new one
suggestedCitiesWorkItem?.cancel()
let workItem: DispatchWorkItem = DispatchWorkItem {
import UIKit
class SplashViewController: UIViewController {
var launchDataDispatchGroup: DispatchGroup = DispatchGroup()
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.global().async { [weak self] in
self?.getAppLaunchData()
}
import UIKit
class SplashViewController: UIViewController {
var launchDataDispatchGroup: DispatchGroup = DispatchGroup()
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.global().async { [weak self] in
self?.getAppLaunchData()
}
import Foundation
// Perform loop 1 to 10 on global concurrent queue with 'userInteractive' QoS
DispatchQueue.global(qos: .userInteractive).async {
for i in 1...10 {
print("Queue 1: \(i)")
}
}
// Perform loop 11 to 20 on global concurrent queue with 'userInitiated' QoS
import UIKit
extension UILabel {
func updateLabel(with newText: String) {
DispatchQueue.main.async {
// Update the text of the label on the main thread
self.text = newText
}
}
}
const Parent = () => {
const [count, setCount] = useState<number>(0);
const printFunction = useCallback((text: string) => {
console.log(text);
}, []);
// some other functions
return (
type props = {
callback: (a: string) => void;
}
const Child: React.FC<props> = ({callback}) => {
const [value, setVlaue] = useState<number>(0);
const isEven = () => {
let i = 0;
// this while loop is to mock a costly operation
const Parent = () => {
const [count, setCount] = useState<number>(0);
const printFunction = (text: string) => {
console.log(text);
}
// some other functions
return (