I’m new to Erlang and learning about it by reading through “Programming Erlang” (thank you Joe Armstrong for this excellent book). I just finished chapter 8 and was mulling over the first of the programming exercises from section 8.11:
bq. Write a function start(AnAtom, Fun) to register AnAtomas spawn(Fun). Make sure your program works correctly in the case when two parallel processes simultaneously evaluate start/2. In this case, you must guarantee that one of these processes succeeds and the other fails.
I was stumped at first, but then I found the following discussion post, “Programming Erlang Exercise 8.11”:http://www.nabble.com/Programming-Erlang-Exercise-8.11-tf4485540.html#a12791301 , which presented a seemingly logical solution.
The solution in that thread did raise 2 interesting questions for me that I wanted to throw out to any experienced Erlang programmers:
How is the BIF register/2 function implemented to be an atomic call? I looked in the Erlang documentation online and could not find any details. As a BIF it is implemented in C, so I suppose there a lot of possibilities, but is there any way for a curious person to find out?
Is this problem of multiple processes trying to call register/2 at the same time something that you (experienced with Erlang) run into a lot and have to code around or is this problem more theoretical?