In Erlang, what's the difference between gen_server: start () and gen_server: start_link ()?

Can someone explain what is the difference between gen_server:start() and gen_server:start_link() ?

I was told that this is something about multithreaded stuff.

EDIT: If my gen_server is called from multiple threads, will it execute all at once? Or will it create concurrency between these threads?

+5
source share
2 answers

Both functions start new instances of gen_server as children of the calling process, but they differ in that gen_server:start_link/3,4 atomically starts the child of gen_server and the link is its parent process. Linking means that if a child dies, the parent also dies by default. Supervisors are parent processes that use links to take action when their child processes fail, usually reloading them.

In addition to the connection associated with the gen_server:start_link , there are no multiprocessing aspects in these calls. Regardless of whether you gen_server:start or gen_server:start_link to start the new gen_server , the new process has one message queue and it receives and processes these messages one at a time. Nothing happens in gen_server:start_link , which leads to the fact that the new gen_server process behaves or works differently than if it started with gen_server:start .

+9
source

When you use gen_server:start_link , the new process becomes the "child" of the call process - it is part of the observation tree. This allows the invocation process to be called if the gen_server process dies.

Using gen_server:start will cause the process to appear outside the observation tree.

A good description of surveillance in Erlang is here: http://learnyousomeerlang.com/supervisors

+4
source

All Articles