Couldn't this part of this article (image below) <...
# coroutines
j
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
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
I don't see how a semaphore is any better, select also suspends until one of the workers frees up if used correctly
j
@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
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
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
thanks guys!