Questions tagged [structured-concurrency]

Structured concurrency is a style of multi-threaded programming where concurrent subtasks are spawned within a code block, and they will all return to the same place: the end of the code block. The name is related to 'structured programming', in that the syntactic structure of the code has meaning.

22 questions
11
votes
3 answers

How to properly cancel Swift async/await function

I have watched Explore structured concurrency in Swift video and other relevant videos / articles / books I was able to find (swift by Sundell, hacking with swift, Ray Renderlich), but all examples there are very trivial - async functions usually…
9
votes
1 answer

why is it legal to mutate an actor's nonSendable property?

The following code is legal in Swift 5.5 (beta): class Dog { var name = "rover" var friend : Dog? = nil } actor MyActor { let dog = Dog() } func test() async { let act = MyActor() act.dog.name = "fido" act.dog.friend = Dog() …
matt
  • 515,959
  • 87
  • 875
  • 1,141
6
votes
1 answer

Is there a difference between using GlobalScope.launch and CoroutineScope().launch to launch a coroutine?

There are different ways of launching a coroutine in Kotlin. I found a couple of examples where GlobalScope and CoroutineScope are used. But the latter one is being created directly when launching a coroutine: Using GlobalScope: fun…
Sergio
  • 27,326
  • 8
  • 128
  • 149
6
votes
1 answer

Swift - Not wait for async to return

I'd like to be able to let the function doSomething() from class B to not be async and to not block it's caller thread. But with the following code I get this error: Cannot pass function of type '() async -> Void' to parameter expecting synchronous…
barola_mes
  • 1,532
  • 2
  • 13
  • 16
5
votes
1 answer

In Loom, can I use virtual threads for Recursive[Action/Task]?

Is it possible to use RecursiveAction, for example, in conjunction with -- instead of the fork/join pool -- a pool of virtual threads (before I attempt a poorly-designed, custom effort)?
HellishHeat
  • 2,280
  • 4
  • 31
  • 37
5
votes
1 answer

Why does a Task within a @MainActor not block the UI?

Today I refactored a ViewModel for a SwiftUI view to structured concurrency. It fires a network request and when the request comes back, updates a @Published property to update the UI. Since I use a Task to perform the network request, I have to get…
BlackWolf
  • 5,239
  • 5
  • 33
  • 60
4
votes
1 answer

DispatchQueue.main.asyncAfter equivalent in Structured Concurrency in Swift?

In GCD I just call: DispatchQueue.main.asyncAfter(deadline: .now() + someTimeInterval) { ... } But we started to migrate to Structured Concurrency. I tried the following code: extension Task where Failure == Error { static func delayed( …
4
votes
1 answer

How can I indicate I want the synchronous version of a function when in an async context in Swift?

If you are using structured concurrency in Swift, and are in an async context, the compiler will assume that if an async version of a function exists, that is the one you want to use. Is there a way to tell it you want the synchronous version? Here…
WillyC
  • 3,917
  • 6
  • 35
  • 50
3
votes
1 answer

Kotlin: Difference between calling CoroutineScope.launch vs launch inside a coroutine

I am trying to understand structured concurrency in Kotlin and I am unable to wrap my head across this piece of code. fun main(): Unit = runBlocking { other(this) } suspend fun other(scope: CoroutineScope) { val job = scope.launch { …
varunkr
  • 5,364
  • 11
  • 50
  • 99
3
votes
2 answers

How can I know if a Java 19 Structured Concurrency StructuredTaskScope is shutdown ( canceled )?

A Java 19 ShutdownOnFailure scope also allows for explicit cancellation of all tasks using the shutdown method. How can I know if the scope has been shutdown? The API includes an isShutdown method, but it is private. Here is some (incomplete) code…
Gonen I
  • 5,576
  • 1
  • 29
  • 60
3
votes
2 answers

How to resume a continuation ensuring that the result is delivered on the MainActor?

I have a continuation: func a() async -> Int { await withCheckedContinuation { continuation in continuation.resume(returning: 3) } } I would like all callers of this function to receive the result on the MainActor. I wouldn't like…
Isaaс Weisberg
  • 2,296
  • 2
  • 12
  • 28
2
votes
1 answer

Where should we use "Task {}": in ViewModel or ViewController?

Let's suppose we have some asynchronous code. At some point we must wrap it in a Task {…} in order to run it from a synchronous context. So where is the canonical way to do so? ViewModel or ViewController? If we wrap it with Task {…} in ViewModel,…
Roman
  • 1,309
  • 14
  • 23
2
votes
2 answers

TaskGroup limit amount of memory usage for lots of tasks

I'm trying to build a chunked file uploading mechanism using modern Swift Concurrency. There is a streamed file reader which I'm using to read files chunk by chunk of 1mb size. It has two closures nextChunk: (DataChunk) -> Void and completion: () -…
2
votes
1 answer

Passing a local mutable struct into an async let binding

I want to create a URL request and pass it into an async let binding, which seems natural to me: func test() async { // Force unwraps (!) are just for demo var request = URLRequest(url: URL(string:"https://stackoverflow.com")!) …
FreeNickname
  • 7,398
  • 2
  • 30
  • 60
1
vote
0 answers

How to pass the CoroutineScope to composed objects in Kotlin (Structured Concurrency)

I have a Kotlin application main class which provides a CoroutineScope. The main class creates various instances of other classes (composition), all of which should use the scope from the main class in order to implement proper structured…
macnixde
  • 213
  • 1
  • 9
1
2