practice
Doi: 10.1145/1467247.1467263
Designed for concurrency from the ground
up, the Erlang language can be a valuable tool
to help solve concurrent problems.
BY Jim LaRson
erlang for
concurrent
Programming
Erlang is a language developed to let mere mortals
write, test, deploy, and debug fault-tolerant concurrent
software. Developed at the Swedish telecom company
a
Ericsson in the late 1980s, it started as a platform
for developing soft real-time software for managing
phone switches. 1 It has since been open sourced and
ported to several common platforms, finding a natural
fit not only in distributed Internet server applications,
but also in graphical user interfaces and ordinary
batch applications.
Erlang’s minimal set of concurrency primitives,
together with its rich and well-used libraries, give
guidance to anyone trying to design a concurrent
program. Erlang provides an effective platform for
concurrent programming for the following reasons:
˲ The language, the standard libraries (Open
Telecom Platform, or OTP), and the tools have been
designed from ground up for supporting concurrency.
˲ There are only a few concurrency primitives, so it’s
a See Erlang Web site www.erlang.org.
easy to reason about the behavior of
programs (though there are limits to
how easy this can ever be).
˲ The implementation makes the
simple primitives fast and scalable,
and makes effective use of modern
multicore hardware, eliminating the
need for more complex mechanisms.
˲ The execution model eliminates
some classes of errors from unsynchronized access to shared state—or at least
makes these errors more noticeable.
˲ The model of concurrency is natural to think about and requires no
mathematical sophistication.
˲ The environment makes failures
detectable and recoverable, making it
possible to deploy a less-than-perfect
system in the field that can nonetheless maintain high availability.
˲ The concurrency model maps naturally to distributed deployments.
This article introduces the Erlang
language and shows how it can be used
in practice to implement concurrent
programs correctly and quickly.
sequential erlang
Erlang is built from a small number
of sequential programming types and
concepts, and an even smaller number of concurrent programming types
and concepts. Those who want a full
introduction can find several excellent
tutorials on the Web, b but the following
examples (required by functional programming union regulations) should
convey the essentials.
As shown in Figure 1A, every file
of Erlang code is a module. Declarations within the file name the module
(which must match the filename) and
declare which functions can be called
from other modules. Comments run
from the percent sign (%) to the end of
the line.
Factorial is implemented by two
functions. Both are named factorial, but they have different numbers
of arguments; hence, they are distinct.
The definition of factorial/2 (the
illustration By andy gilmore
b See Erlang course www.erlang.org/download/
armstrong_thesis_2003.pdf.