https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
s

stoyicker

03/15/2024, 12:16 PM
Hi, does anybody know if there's up-to-date documentation on the relationship between coroutines and dispatch queues? I am using a native API that requires a dispatch queue and I would like to provide one based on the current coroutine context. Unfortunately I have no clue on how to do this because all documentation I find is about the old memory model
n

natario1

03/15/2024, 5:47 PM
A
CoroutineDispatcher
may not be backed by a dispatch queue. The only way I see to get a dispatch queue out of a dispatcher, is to create your own dispatcher implementation with a
queue
member variable. (Not sure about documentation links)
s

stoyicker

03/15/2024, 8:53 PM
mm that's a little unfortunate, I think it would be a reasonable expectation to have from the framework, but I guess it is what it is
thanks anyway
j

Jeff Lockhart

03/15/2024, 9:25 PM
I created this extension function to use in my library to get a dispatch queue for a coroutine dispatcher. It either returns the main dispatch queue for the main coroutine dispatcher, or else creates a serial dispatch queue to use for any other coroutine dispatcher. Note, this solution is somewhat fragile and relies on assumptions with how it's used. It may create a new dispatch queue each time it's called. But if you only call it once for a given usage, as I'm doing in my library, it does at least handle the case of getting a valid serial dispatch queue for either main or background work, depending on the coroutine context.
2 Views