Concurrent Programming in Erlang - You've Got Mail (week one)

Full article

This past weekend was great weather-wise. Sunny and 70. Summer's around the corner but in Ohio there are never any guarantees... the weekend before it snowed 4 inches. We had places to go and errands to run (as always), but I got some work done outside, took the kids on a bike ride, and met some new neighbors down the street who were cutting up trees they'd taken down (free firewood!). Sunday ended with a nice cookout, a good vanilla stout, and watching "Fantastic Beasts and Where to Find Them" with the kids. All-in-all, it was pretty awesome. This is always the lull for us, between Christmas and Easter... then the holidays, picnics and birthdays start up again in full force.

I always wish I could get more done though. Who wouldn't like to be able to do several things at once? But where people generally stink at multitasking, computers shouldn't. I'm taking a class right now called Concurrent Programming in Erlang, all about how to do many things (potentially thousands or even millions) at once, and how it's built into the language. If you've already got some Erlang experience, jump in. Week 2 (of 3) started, but they'll probably leave the course open for a few weeks past the finish.

We've only scraped the tip of the iceberg so far, but here's some of what I've learned.

Concurrency in Erlang (the basics)

Take a very simple function like the following, which accepts one parameter (a message), and then pattern-matches on it to determine what action to perform.

parrot(Message) ->
  case Message of
    hello -> io:format("Hi to you too!~n");
    Msg -> io:format("Your message: ~p~n", [Message])

But that method is running in the same process that called it. How do we take advantage of concurrency and run it in its own thread/process? Replace the case with a receive and remove the parameter.

parrot() ->
    hello -> io:format("Hi to you too!~n");
    Message -> io:format("Your message: ~p~n", [Message])

Then how does it get the message? When a message is sent to the process executing this function, it's placed in a mailbox (you get one per process for free). Once it's running (I'll get to that in a sec), the above function is suspended at the receive, waiting for a message to get delivered to the mailbox, and when it does it matches the pattern and performs an action. (The last call to parrot() just makes sure it's waiting for the next message again.)

All that's left is to start the function in a separate process - spawn it - and send it messages:

Parrot = spawn(talk, parrot, []).
Parrot ! "Testing 1.. 2.. 3"

It gets more complicated than that of course, but I was impressed with how easy it was to get moving out of the gate. Instead of just displaying messages, the "action" could be to do some calculation, call another function, or even spawn another process that waits for its own messages.


If you sign up for the course, be sure to leave some time to skim through the comments. There are some people taking the course who seem to have a lot of previous functional programming experience, and some of the discussions they're having and resources they're sharing have been helpful in addition to the course itself.

Here's some resources that have been passed around in the forum (some are from last month's class). There's a lot so I kinda haphazardly threw them into categories. Mostly I just wanted to make a note of them somewhere.

Starting Out...

More advanced..?

Testing is good


Other tools and related

The University of Kent has three classes that teach progressively harder concepts, and they've posted the channels to their videos.

If you want to learn more about Erlang, join the class. I'd also suggest starting with Learn You Some Erlang and checking out the articles on Spawned Shelter. Good luck!


Grant Winney

I write when I've got something to share - a personal project, a solution to a difficult problem, or just an idea. We learn by doing and sharing. We've all got something to contribute.

Comments / Reactions