[erlang-questions] Executing concurrently

Hakan Mattsson <>
Tue Mar 3 19:10:55 CET 2009


Of course you may implement your own lock manager as Joe suggests, but
a simpler solution is to just call global:trans/4 with Retries set to 0.
It will perform the syncronization you need. (Given that the performance
of global is sufficient for your needs.)

If you are using Mnesia transactions in your fun, you have the option of
using global locks in Mnesia (see mnesia:lock/2).

/Håkan
---
Håkan Mattsson (uabhams)
Erlang/OTP, Ericsson AB

On Tue, 3 Mar 2009, Joe Armstrong wrote:

> Date: Tue, 3 Mar 2009 17:41:51 +0100
> From: Joe Armstrong <>
> To: ryeguy <>
> Cc: erlang-questions <>
> Subject: Re: [erlang-questions] Executing concurrently
> 
> On Tue, Mar 3, 2009 at 3:33 PM, ryeguy <> wrote:
> > He is saying that he only wants one process to be able to call his fun
> > at a time. In the event that 2 processes attempt to execute the fun at
> > the same time, one of them will fail while the other one works as
> > normal.
> 
> 
> 
> Heres a fun:
> 
>       F = fun() -> 42 end.
> 
> Evaluating F() returns 42
> 
> Now spawn two processes
> 
>       P1 =  spawn(fun() -> p(F) end),
>       P2 =  spawn(fun() -> p(F) end).
> 
> here's p(F)
> 
>        p(F) ->
>              magic:magic(F).
> 
>  magic:magic(F) returns 42 or calls exit(ebadArg) if two processes
> call magic(F) "at the same time"
> 
>  To do this you need a global process called magic, which is started
> before you do any of this
> 
>   magic:start() -> register(magic, wait()).
> 
>    magic:magic(F) ->
>            magic ! {eval, self(), F},
>            receive
>                 {magic, Ret} -> Ret
>            end.
> 
> 
>    wait() ->
>        receive
>              {eval, Pid, F} ->
>                   S = self(),
>                   spawn(fun() ->
>                                   Pid ! {magic, F()},
>                                   Parent ! free
>                               end),
>                   busy()
>       end.
> 
> busy() ->
>       receive
>             {eval, Pid, F} ->
>                  exit(Pid, eNotSim),
>                  busy();
>              free ->
>                   wait()
>       end.
> 
> Or you could send a lock token and evaluate the function locally - all
> of this does not seem like good design.
> 
> The Erlang way is to create servers for global operations and send a
> sequental stream of requests to the server, where they are handled in
> an orderly manner one at a time.
> 
> Cheers
> 
> /Joe
> 
> >
> > On Mar 3, 7:09 am, Andras Georgy Bekes <> wrote:
> >> >    I have a function Fun(). If Fun is called simultaneously by two
> >> > processes, then it should fail for one process while should succeed
> >> > for other. I wrote such function. Now I want to test it! How do I do
> >> > that? How do I call a function exactly at same time from two
> >> > different processes?
> >>
> >> It depends on how you define "called simultaneously".
> >>
> >> What do you mean "called simultaneously"?
> >> What do you want to achieve?
> >>
> >>         Georgy


More information about the erlang-questions mailing list