Alessandro Diaferia
12/11/2019, 3:10 PMCoroutineScope
that works off an IO dispatcher (<http://Dispatchers.IO|Dispatchers.IO>
) and now I’d need to use a class from an external library that is not thread-safe and requires a heavy initialization phase (in the order of a few seconds) per thread. How should I approach this problem? Would I simply initialize one instance and add it to the current CoroutineContext
? How would its access be regulated to make sure there is no concurrent multi-thread access from the various coroutines spread across multiple threads (I assume <http://Dispatchers.IO|Dispatchers.IO>
spans across multiple threads). Any help appreciated.Alessandro Diaferia
12/11/2019, 3:11 PMThreadLocal
-based initialization but I’d be interested in a more idiomatic coroutine-ish approachbezrukov
12/11/2019, 3:13 PMMutex
primitive https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.sync/-mutex/Alessandro Diaferia
12/11/2019, 3:15 PMKevin Gorham
12/11/2019, 3:22 PMflow
, perhaps by using a BroadcastChannel
for communication. Meaning, you would send messages into the channel and then consume them as a flow, wherever needed.Alessandro Diaferia
12/11/2019, 3:34 PMflow
applies in this case. My main constraint is that I have to be sure a different instance of this library has been initialized per thread. Once that’s done, I should be able to use it safely from any coroutine. So basically, every time my coroutine starts or resumes I have to make sure the library instance being used has been initialized.Kevin Gorham
12/11/2019, 3:41 PM