← Back to Swift Course | Chapter 13: Concurrency | Lesson 5 of 6

Structured Concurrency क्या है

Structured concurrency का मतलब है कि काम के related pieces एक साथ neatly group किए गए हैं, ताकि वे सभी एक tidy unit के रूप में खत्म (या सभी cancelled) हों, अपने आप अलग बिखरने के बजाय।
Syntax
swift
async let first = asyncFunction1()
async let second = asyncFunction2()
let results = await (first, second)

await withTaskGroup(of: ResultType.self) { group in
    group.addTask { await work() }
    for await result in group { /* use result */ }
}

Concurrent Work with async let

async let एक asynchronous operation तुरंत शुरू करता है और इसे function के बाकी हिस्से के साथ concurrently चलने देता है, जब तक इसकी value await न हो।

उदाहरण: Concurrent Work with async let

markup
func fetchFirstName() async -> String {
    return "Ada"
}
func fetchLastName() async -> String {
    return "Lovelace"
}

async let first = fetchFirstName()
async let last = fetchLastName()
let fullName = await "\(first) \(last)"
print(fullName)

Grouping Tasks with withTaskGroup

withTaskGroup आपको child tasks की एक dynamic संख्या जोड़ने देता है और उनके results को एक structured, awaitable unit के रूप में एक साथ collect करने देता है।

उदाहरण: Grouping Tasks with withTaskGroup

markup
func square(_ n: Int) async -> Int {
    return n * n
}

let total = await withTaskGroup(of: Int.self) { group in
    for number in 1...4 {
        group.addTask {
            await square(number)
        }
    }
    var sum = 0
    for await result in group {
        sum += result
    }
    return sum
}
print("Sum of squares: \(total)")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Related work के लिए async let या एक task group से group करने के बजाय कई independent Task { } blocks launch करना, cancellation और error propagation को manage करना मुश्किल बनाते हुए।
  2. यह भूल जाना कि async let bindings declare होते ही concurrently चलती हैं, लेकिन हर एक को अपना result retrieve करने के लिए अब भी await किया जाना चाहिए।
  3. एक task group उपयोग करना लेकिन group के with...TaskGroup block खत्म होने से पहले इसकी सभी child tasks को await करना भूल जाना।
चैप्टर सारांश
  • async let एक function scope के अंदर कई asynchronous operations concurrently शुरू करता है।
  • हर async let binding को इसकी value retrieve करने के लिए await किया जाना चाहिए।
  • Task groups (withTaskGroup) concurrent child tasks की एक dynamic संख्या को एक साथ manage करते हैं।
  • Structured concurrency किसी task की lifetime को इसे बनाने वाले scope से बांधती है, cancellation और error handling को predictable बनाते हुए।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.