Exercise 5-3: atomic operation

120 views
Skip to first unread message

Ivan Uemlianin

unread,
Apr 4, 2010, 1:48:32 PM4/4/10
to Erlang Programming
Dear All

How do you ensure an operation is atomic? I read in the book (p. 147)
that all BIFs are atomic, but I'm note sure how user-defined
operations can be made atomic. As a first bash, I have tried to cram
what needs to be done into one operation on one line. Here's my
handle_msg for swap_handlers:

handle_msg({swap_handlers, OldHandler, NewHandler}, LoopData) ->
case lists:keysearch(OldHandler, 1, LoopData) of
false ->
{{error, instance}, LoopData};
{value, {OldHandler, Data}} ->
NewLoopData = [ { NewHandler,
NewHandler:init(OldHandler:terminate(Data)) } |
lists:keydelete(OldHandler, 1, LoopData) ],
{ok, NewLoopData}
end;

The "NewLoopData = " line adds the new handler and deletes the old
handler in one list-building operation. Is it atomic?

With thanks and best wishes

Ivan

p.s.: And wouldn't it be simpler just to add the new handler before
deleting the old one? ;)

Ale

unread,
Apr 4, 2010, 2:10:11 PM4/4/10
to erlang-prog...@googlegroups.com
2010/4/4 Ivan Uemlianin <iv...@llaisdy.com>:

> Dear All
>
> How do you ensure an operation is atomic?

Does it really matter in this case? I mean even if there is preemption
of the process the event_manager has to receive a message and the then
dispatch the message to the handler, but before it can receive a new
message it has to complete what ever it was doing, it has to go back
to the loop. Also consider that the messages are async. So the
'send_event' message will wait in the queue till the event_manager
handles it.

Am I right?

Cheers,
--
Ale.

Ivan Uemlianin

unread,
Apr 5, 2010, 2:58:06 AM4/5/10
to Erlang Programming
Dear Ale

Thanks. This sounds right for the current application.

I'd still like ot know if there's a general way of guaranteeing
atomicity, or if (like here( it has to be worked out for each
application.

Best

Ivan

On Apr 4, 7:10 pm, Ale <peralta.alejan...@gmail.com> wrote:
> 2010/4/4 Ivan Uemlianin <i...@llaisdy.com>:

Jaime Enriquez

unread,
Feb 21, 2013, 11:12:43 AM2/21/13
to erlang-prog...@googlegroups.com
Hello Ivan,

I'm trying to solve this exercise: Swapping Handlers and reading your code and taking into account the exercise literally, what happens if you try to swap the log_handler?. In the log_handler code, the terminate/1 function closes the file and returns an 'ok' atom, now in your code, if you pass that 'ok' to the log_handler:init/1 function, what would happen? considering that you want to open a new file with another name?, however, if you were to swap the io_handler then there will be, apparently. no problem since its terminate/1 function returns the tuple {count, Count} and its init/1 function receives as an argument a whatever you want to pass but the new format of the variable Count would be, for instance, {count, 5} and not 5 only. I hope you understand all this and if I am wrong then right me please. Thanks.
Reply all
Reply to author
Forward
0 new messages