https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

James

11/07/2019, 5:17 PM
Couldn't this part of this article (image below) https://proandroiddev.com/kotlin-coroutines-channels-csp-android-db441400965f be done with a semaphore much easier? I get that channels can help when your application gets bigger, but i'm wondering if channels are a bit much for this problem. With a semaphore implementation the methods would still suspend until one of the workers could be freed up
c

Casey Brooks

11/07/2019, 5:22 PM
The coroutines APIs do have some of these lower-level constructs available, including both semaphores and mutexes https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.sync/index.html
o

octylFractal

11/07/2019, 5:23 PM
I don't see how a semaphore is any better, select also suspends until one of the workers frees up if used correctly
j

James

11/07/2019, 5:33 PM
@Casey Brooks thats what i'm talking about. the coroutines semaphore. @octylFractal so if semaphores aren't any better (which is totally valid, im trying to understand), can you explain how a channel is better for this purpose?
o

octylFractal

11/07/2019, 5:34 PM
because it also allows data flow between the coroutines? a semaphore can't do that without additional objects, adding to the complexity of that code. any further and you're basically rebuilding channels
👆 1
c

Casey Brooks

11/07/2019, 5:35 PM
A channel is an abstraction over communication between coroutines. They help you not need to think about concurrency primitives, and instead just focus on sending/receiving data. Normal thread synchronization techniques are notoriously error-prone because these primitives are difficult to use correctly, and channels help you avoid using those primitives while still having safe, concurrent code
j

James

11/07/2019, 5:48 PM
thanks guys!
2 Views