Laxystem
12/21/2024, 12:06 PMDispatcher.Default
to use that thread. Is that possible? From my understanding, newSingleThreadContext
would not allow this.Sam
12/21/2024, 12:33 PMDispatchers.Default.limitedParallelism(1)
might be what you want. It won't guarantee to always use the same thread, though.Laxystem
12/21/2024, 12:33 PMSam
12/21/2024, 12:42 PMDispatchers.Main
in Android and Swing.Sam
12/21/2024, 12:42 PMLaxystem
12/21/2024, 12:46 PMkevin.cianfarini
12/21/2024, 12:48 PMLaxystem
12/21/2024, 12:49 PMLaxystem
12/21/2024, 12:49 PMSam
12/21/2024, 1:01 PMLaxystem
12/21/2024, 1:18 PMDispatchers.Main
-when-needed approach simply wouldn't change anything: the main loop barely has any logic in it.
Now, I don't absolutely need that extra thread, but in practice, it is wasted: it is only used once per frame to poll input from the system. And on low-end machines, this can be a significant impact to performance.Laxystem
12/21/2024, 1:19 PMSam
12/21/2024, 2:06 PMLaxystem
12/21/2024, 2:08 PMDmitry Khalanskiy [JB]
01/07/2025, 2:12 PMkotlinx.coroutines
at the moment, but with https://github.com/Kotlin/kotlinx.coroutines/pull/4208 (expected to land in the next major version), it should be possible to have a GlfwContextElement
that automatically registers and unregisters a thread that's currently running the coroutine as the one where the context is current.Laxystem
02/20/2025, 7:24 AMLaxystem
02/20/2025, 7:25 AMLaxystem
02/20/2025, 7:25 AMLaxystem
02/20/2025, 7:46 AMLaxystem
02/20/2025, 7:46 AMDmitry Khalanskiy [JB]
02/20/2025, 7:55 AMLaxystem
02/20/2025, 7:56 AMLaxystem
02/20/2025, 7:56 AMDmitry Khalanskiy [JB]
02/20/2025, 9:03 AMDispatchers.IO.limitedParallelism(1)
to create a dispatcher that will run at most one coroutine at a time. As long as all coroutines with your context element use the same limited dispatcher, they won't be in conflict.Laxystem
02/20/2025, 10:25 AMlimitedParallelism
(been some time since I messed with this). Thank you so much!Laxystem
02/25/2025, 9:43 AMDmitry Khalanskiy [JB]
02/25/2025, 10:01 AMLaxystem
02/25/2025, 10:02 AMDmitry Khalanskiy [JB]
02/25/2025, 10:14 AMmain
function of the program. So, newSingleThreadContext
wouldn't help you, as it spawns a new thread, and similarly, you are not allowed to use Dispatchers.Default
or <http://Dispatchers.IO|Dispatchers.IO>
.
The only thing you can do to use coroutines in this scenario at all is to do something like this:
lateinit var glfwDispatcher: CoroutineDispatcher
fun main() {
runBlocking {
glfwDispatcher = coroutineContext[CoroutineDispatcher.Key]!!
delay(Duration.INFINITE) // do not exit the main thread
}
}
Then, using glfwDispatcher
to run some tasks will make sure they happen on the thread calling main()
.Laxystem
02/25/2025, 10:15 AMLaxystem
03/10/2025, 4:41 PMDmitry Khalanskiy [JB]
03/11/2025, 12:31 PMLaxystem
03/11/2025, 12:44 PM