[erlang-questions] Regarding clearing gen_server state

Steve Vinoski <>
Sat Feb 19 22:23:54 CET 2011


On Sat, Feb 19, 2011 at 3:40 PM, Edward Wang <> wrote:
> Hi,
>
> In Erlang, it is quite ubiquitous to use state of dynamically spawned
> gen_server to represent entities in problem domain. These gen_servers can
> trap exit signal and do housekeeping when gracefully terminating. If some of
> them crash, a monitoring process can take care of housekeeping for them.
> But, the problem is most likely the monitoring process doesn't have enough
> data to do so. No shared state, remember?
>
> So, I end up with two copy of the same data, one in gen_server's state, one
> in ets table. Not only I need to keep them in sync, but also it *is* shared
> data to some extent.
>
> How can I do it differently?

No need to duplicate the data. Just use ets to hold your state, then
see the ets heir option:

http://erlang.org/doc/man/ets.html#heir

and/or the ets give_away function:

http://erlang.org/doc/man/ets.html#give_away-3

both of which allow you to transfer ets ownership to another process
if your ets owner dies, or just consider creating a supervised process
to do nothing else except own a public ets table that the gen_server
can read and write.

--steve


More information about the erlang-questions mailing list