On May 13, 2013, at 1:58 PM, Adit Bhargava <
blueman...@gmail.com> wrote:
> Here are two solutions to this classic problem.
> In the first one, the forks are actors. The way I pick up two forks is by
> calling `*fork.future.lock*` and then waiting for the result:
>
> *
https://gist.github.com/egonSchiele/5565704
> *
>
> This one seems more idiomatic, but I'm blocking when I take the forks,
> correct? So this implementation *could* deadlock (although I haven't seen
> this happen yet).
>
> In this second one, the forks are mutexes. If I can't lock the mutex, I
> just try again later instead of blocking:
>
> *
https://gist.github.com/egonSchiele/5565713
> *
>
> But this seems wrong because Celluloid is supposed to shield me from
> mutexes.
>
> So my question is, how would I make these more idiomatic and correct?
>
> Thanks for your help!
An interesting discussion point indeed!
I would suggest that Celluloid is providing a new way of developing concurrent/parallel applications.
Given this, you'd want to first consider using the high-level constructs, Actors combined with sync and async calls and futures.
If these constructs do not meet your needs, then dropping down and using the primitives inside Celluloid.
These are Conditions, Calls, Mailboxes and friends.
Using the Mutex to manage the locking is breaking the underlying assumptions of Celluloid and using a primitive which Celluloid primitives use themselves.
The first solution is a better solution in my opinion as it uses very high-level constructs.
A more efficient solution might be to use a Condition instead of a Future.
A sidebar would be that over the next while (2 weeks to a month) we'll be hopefully making changes which can show some of the more idiomatic uses of Celluloid primitives.
One of these is the question of bare actors without an object.
https://gist.github.com/tarcieri/5550447
The other would be a Pool/Router built on this bare actor.
https://github.com/halorgium/celluloid-coordinator
I have some examples which I have written and listed on our wiki.
https://github.com/celluloid/celluloid/wiki/Elsewhere-on-the-Internet
I hope this helps. I'd love to see an example of this solution implemented using Condition.
Ciao,
Tim