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

Lulu

11/23/2020, 11:45 PM
We're trying to perform some tasks sequentially and i was wondering if an actor or a Mutex is better suited for this case. There'd be many queues and each of them will be processing few tasks.
z

zak.taccardi

11/23/2020, 11:46 PM
a mutex is the coroutines version of a lock that is non-re-entrant
I find code that uses locks difficult to work with, and the fact that a mutex is non-re-entrant makes it even more difficult
that said, you can make a mutex re-entrant with the code shown here https://elizarov.medium.com/phantom-of-the-coroutine-afc63b03a131
however I love actors and find them delightful to use, but there’s some boilerplate around setting them up
l

Lulu

11/23/2020, 11:49 PM
I know the difference between them but I'm not sure which one is better for this specific case. I'm not very enthusiastic about having hundreds of channels.
z

zak.taccardi

11/23/2020, 11:50 PM
do you need multiple queues? or a single queue that distributes work to multiple “workers”
l

Lulu

11/23/2020, 11:51 PM
Multiple queues
z

zak.taccardi

11/23/2020, 11:51 PM
okay yeah then an actor seems appropriate
does your actor need to shut down after completing its work?
l

Lulu

11/23/2020, 11:52 PM
Possibly, to save memory
z

zak.taccardi

11/23/2020, 11:53 PM
(an actor would just be a coroutine that maintains a queue of work to be done in the form of a channel)
would each actor have its own on-going state?
l

Lulu

11/23/2020, 11:53 PM
Yes
z

zak.taccardi

11/23/2020, 11:54 PM
yeah, an actor definitely seems appropriate then
allows you to define state unique to that actor, separate from other actors
l

Lulu

11/23/2020, 11:55 PM
I see, "the state" is a good point. Thanks for the input
2 Views