<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Recent Posts in 'Problem 8.11 and register/2' | Pragmatic Forums</title>
    <link>http://forums.pragprog.com/forums/27/topics/124</link>
    <language>en-us</language>
    <ttl>60</ttl>
    <description></description>
    <item>
      <title>Problem 8.11 and register/2 posted by Peter Miller @ Thu, 20 Dec 2007 05:00:14 -0000</title>
      <description>&lt;p&gt;Agreed. Thanks for sharing your version of the solution.&lt;/p&gt;</description>
      <pubDate>Thu, 20 Dec 2007 05:00:14 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:27:124:2054</guid>
      <author>Peter Miller</author>
      <link>http://forums.pragprog.com/forums/27/topics/124</link>
    </item>
    <item>
      <title>Problem 8.11 and register/2 posted by Alain O'Dea @ Mon, 10 Dec 2007 03:11:36 -0000</title>
      <description>&lt;p&gt;I am new to Erlang as well and this problem had me so stumped that I nearly abandoned the book altogether. Years of using Object-oriented programming languages with bolted on concurrency support had left me with limited problem solving skills for concurrent problems.&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;Erlang&amp;#8217;s source is available and it should contain the implementation of register/2&lt;/li&gt;
		&lt;li&gt;I imagine this is a common problem. After looking at Ladislav Lenart&amp;#8217;s solution to 8.11 I am happy that there is a relatively concise way to solve it&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;Here is my version of Ladislav Lenart&amp;#8217;s solution altered to behave like register/2 in terms of exceptions thrown and return values:&lt;br /&gt;&lt;pre&gt;
-module (start_register).
-export ([start/2]).

start(Atom, Fun) -&amp;gt;
    Registrant = self(),
    spawn(
        fun() -&amp;gt;
            try register(Atom, self()) of
                true -&amp;gt;
                    Registrant ! true,
                    Fun()
            catch
                error:badarg -&amp;gt;
                    Registrant ! false
            end
        end),
    receive
        true  -&amp;gt; true;
        false -&amp;gt; erlang:error(badarg)
    end.
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;This looks like the basis of a atomic transaction design pattern for Erlang. I imagine a collection of such design patterns exists. I would really like to see it if it does.&lt;/p&gt;</description>
      <pubDate>Mon, 10 Dec 2007 03:11:36 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:27:124:2007</guid>
      <author>Alain O'Dea</author>
      <link>http://forums.pragprog.com/forums/27/topics/124</link>
    </item>
    <item>
      <title>Problem 8.11 and register/2 posted by Peter Miller @ Mon, 29 Oct 2007 04:59:11 -0000</title>
      <description>&lt;p&gt;I&amp;#8217;m new to Erlang and learning about it by reading through &amp;#8220;Programming Erlang&amp;#8221; (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:&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;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&lt;br /&gt;other fails.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;I was stumped at first, but then I found the following discussion post, &lt;a href="http://www.nabble.com/Programming-Erlang-Exercise-8.11-tf4485540.html#a12791301"&gt;Programming Erlang Exercise 8.11&lt;/a&gt; , which presented a seemingly logical solution.&lt;/p&gt;


	&lt;p&gt;The solution in that thread did raise 2 interesting questions for me that I wanted to throw out to any experienced Erlang programmers:&lt;/p&gt;


	&lt;p&gt;1. How is the &lt;span class="caps"&gt;BIF&lt;/span&gt; register/2 function implemented to be an atomic call? I looked in the Erlang documentation online and could not find any details. As a &lt;span class="caps"&gt;BIF&lt;/span&gt; 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?&lt;/p&gt;


	&lt;p&gt;2. 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?&lt;/p&gt;</description>
      <pubDate>Mon, 29 Oct 2007 04:59:11 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:27:124:1889</guid>
      <author>Peter Miller</author>
      <link>http://forums.pragprog.com/forums/27/topics/124</link>
    </item>
  </channel>
</rss>
