Erlang/OTP Forums

Author Message

<  Erlang questions mailing list  ~  RAII in Erlang?

Radagast
Posted: Mon Jan 30, 2012 2:12 pm Reply with quote
Joined: 30 Jan 2012 Posts: 1
Hello!
What are the best approaches to use RAII (Resource allocation is initialization) idiom in Erlang at the level of a function scope or try-catch-after scope? in other words, how do I release resources automatically when execution leaves a function either normally or throwing an exception?
thanks in advance
View user's profile Send private message
dehunter
Posted: Mon Feb 06, 2012 9:37 am Reply with quote
Joined: 23 Jun 2011 Posts: 1
You should have 2 processes for this. One is worker and second is "resource watcher". You can do all resource initializations in "resource watcher" and then run you worker process. Then when something fail in worker - you just do all deallocations in "resource watcher"(it will be notified when linked process die). Then resource watcher also dies.
But your "resource watcher" can also die in deallocation procedure.
But usual you can avoid all this - cos in erlang you have garbage collector.
If you have some hard initializations - try to review you architecture - maybe there are other way which will simplify things.
View user's profile Send private message ICQ Number

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