Question

I'm currently in a project making a gameserver. Every player is supposed have an own process with gen_fsm behavior. To start gen_fsm with start/3 we need a unique atom, currently we use make_ref() and have found a way to make this ref into an atom with ref_to_list/1. But according to this post it is not recommended and I agree with him.

How would you solve this problem? When we start a gen_fsm with the local option we need an atom to identify it.

Was it helpful?

Solution

If you use gen_fsm:start/3 the only atom you need is the callback module name. That way you only have to keep track of a PID (process ID) which will automatically be unique.

If you need to reach the gen_fsm process later, either save the PID in some sort of mapping table or name the process (as you did).

OTHER TIPS

Maybe I'm missing something, but it sounds like your best course of action would be to not specify the local option, i.e. to not give the gen_fsm process a name.

It's worth noting that there is a limit to the number of unique atoms that an instance of the erlang vm can use. So generating lots of random atoms is probably a bad idea.

You should see gproc (https://github.com/esl/gproc) to create process registry for associating some erlang term with pid() of process. It is not good idea to register each started gen_fsm process with unique atom.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top