# Concurrency 101

**URL:** <https://forums.swift.org/t/concurrency-101/74161>\
**Category:** Using Swift\
**Tags:** concurrency\
**Created:** [August 26, 2024, 2:10am UTC](https://forums.swift.org/t/concurrency-101/74161 "2024-08-26T02:10:18Z")\
**Posts on this page:** 1\
**Showing post:** 32

<div class="post-metadata">

**Author:** ![ibex](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ibex/32/30439_2.png) [@ibex](https://forums.swift.org/u/ibex)\
**Post date:** [September 12, 2024, 12:02am UTC](https://forums.swift.org/t/concurrency-101/74161/32 "2024-09-12T00:02:55Z")

</div>

Quoting from [How to avoid cascading async functions?](https://forums.swift.org/t/how-to-avoid-cascading-async-functions/74494)

> [@How to avoid cascading async functions?](https://forums.swift.org/t/how-to-avoid-cascading-async-functions/74494/11):
>
> Would something like this work?
> 
> ```swift
> func sync_meets_async () {
> ...
> let s = DispatchSemaphore (value: 0)
> Task {
> await async_world ()
> s.signal()
> }
> ...
> print ("waiting for async_world to finish...")
> s.wait()
> print ("finished")
> }
> 
> func async_world () async {
> try! await Task.sleep(until: .now + .seconds(7))
> }
> 
> ```

@j-f1 explains nicely why the above attempt is an anti pattern.

> [@How to avoid cascading async functions?](https://forums.swift.org/t/how-to-avoid-cascading-async-functions/74494/13):
>
> That’s a risky antipattern. If your `sync_meets_async` function is ever called from Swift’s global concurrent pool (for example, from an actor or async function, even indirectly), you will be blocking one of the finite number of threads in that pool on the expectation that another one of the threads will perform some computation later. While you might not notice any negative effects of this when using the pattern only occasionally, it can deadlock your app or server. Unlike GCD, which expects that people may do this kind of thing and allows a concurrent queue to have more threads than there are logical cores on the system, Swift concurrency requires that you never block one task on the expectation of future work occurring on another task (but offers `await` as a way for you to do so safely without blocking a thread). So if you fill all of the threads in the global concurrent pool with tasks that are blocking on future async work using a semaphore, none of that async work can ever be scheduled.

But, is there really no practical solution for this that can be used when the `sync world` meets the `async world` in real life?

**Update** : There is a [safe solution](https://forums.swift.org/t/concurrency-101/74161/37)

---

_[View the full topic](https://forums.swift.org/t/concurrency-101/74161)._
