Skip to content

Instantly share code, notes, and snippets.

@mdinino
mdinino / RunInParallel.kt
Created July 5, 2025 20:06
Runs a list of coroutine execution blocks in parallel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
/**
* Runs a list of coroutine execution blocks in parallel, waits for all of them to complete,
* then returns a list of the return values.
* THis function will fail as soon as a single exception is thrown, and this function will
@mdinino
mdinino / CoroutineCriticalSection.kt
Created May 28, 2025 17:46
A utility class to enable clean FIFO execution within the context of coroutines.
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import kotlinx.coroutines.runBlocking
import kotlin.coroutines.CoroutineContext