Erlang/OTP Forums

Author Message

<  Erlang  ~  semaphore (mutex)

vinnitu
Posted: Fri Sep 10, 2010 2:50 pm Reply with quote
User Joined: 31 May 2007 Posts: 14
i found example of mutex in erlang

Can somebody modify it to use it with count like semaphore?

Thx
Code:

-module(mutex).
-export([start/0, stop/0]).
-export([wait/0, signal/0]).
-export([init/0]).

start() ->
   register(mutex, spawn(?MODULE, init, [])).

stop() ->
   mutex ! stop.

wait() ->
   mutex ! {wait, self()},
   receive ok -> ok end.

signal() ->
   mutex ! {signal, self()},
   ok.

init() ->
   free().

free() ->
   receive
      {wait, Pid} ->
         Pid ! ok,
         busy(Pid);
      stop ->
         terminate()
   end.

busy(Pid) ->
   receive
      {signal, Pid} ->
         free()
   end.

terminate() ->
   receive
      {wait, Pid} ->
         exit(Pid, kill),
         terminate()
   after
      0 -> ok
   end.
View user's profile Send private message
zajda
Posted: Sat Sep 11, 2010 10:47 pm Reply with quote
User Joined: 22 Aug 2009 Posts: 83
For all the other users, here is one possible answer to this question: http://stackoverflow.com/questions/3685987/erlang-semaphore-mutex/3691143#3691143

@vinnitu, I would like to remind you, that posting the same question on many forums is against at least couple rules of netiquette.
View user's profile Send private message
jwatte
Posted: Fri Sep 24, 2010 6:05 pm Reply with quote
User Joined: 10 Feb 2010 Posts: 34
vinnitu wrote:
i found example of mutex in erlang


Writing a process that keeps a count as state is one of the very first Erlang tutorial examples. Adding a list of processes waiting for the semaphore is a minor addition.

However, I think the "Erlang way" is to use a process to "drive" whatever the resource is that you want to serialize, rather than to use a separate synchronization primitive. Or, if your semaphore is counting something like buffers, simply model the producer/consumer relationship directly as communicating processes.
View user's profile Send private message
wojtnar
Posted: Fri Oct 01, 2010 9:37 am Reply with quote
Joined: 05 Feb 2010 Posts: 7
vinnitu wrote:

i found example of mutex in erlang

Can somebody modify it to use it with count like semaphore?



Is this for fun, work or rather homework?

--Wojtek
View user's profile Send private message
wuji
Posted: Tue Sep 04, 2012 7:32 am Reply with quote
User Joined: 10 Aug 2012 Posts: 654
all stay?'" Denniston said.The team agreed."I love swimming under Dave, Dave, replica designer *beep* Dave, and I think he is a good fit," said
Kamber, a member of the Paralympics swimming team. "He understands understands [h2]cheap jordans[/h2] understands each of our training needs."Recently, Denniston was announced as
head coach of the 2012 Paralympic Resident Swim team in in [h4]cheap polo shirts[/h4] in Colorado Springs -- the same position his mentor Flowers
before him.Denniston said that if he had to sum up up [h2]replica designer *beep*[/h2] up his experiences since his 2005 accident in a single
View user's profile Send private message
dongdongwu
Posted: Wed Sep 19, 2012 8:35 am Reply with quote
User Joined: 19 Sep 2012 Posts: 236
Girls would refuse to even leave their replicas behind. specifically when the product beats the complete meaning of the Christian Louboutin men outlet; in conditions of good quality and detailing.If Cinderella was residing in your twenty primary century as opposed to the aged a single then there would not be any 'Happy actually after'. properly appears like girls nowadays are as well fond of the strong;Christian Louboutin Men Shoes to leave them in your center of nowhere.Christian Louboutin for men Shoes would arrive true handy being a excellent handbag using the glimpse and really feel belonging to the authentic but at a very much lesser price tag adding as very much as types picture in your process.
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum