May 13, 2021 Erlang
Compared with other functional programming languages, Erlang's advantage lies in its common programming and distributed programming. A synth is a program in which multiple threads are executing at the same time. F or example, modern operating systems allow you to use word processing, electronic watching software, mail terminals, and printing tasks at the same time. A t any one point, each processing unit (CPU) in the system has only one thread (task) executing, but these threads can be executed alternately at a certain rate to make them look like they are running at the same time. C reating multiple threads in Erlang is simple and it is easy to communicate between these threads. I n Erlang, each thread that executes is called a process (that is, a process, note that it is not quite the same concept as the process in the operating system).
(Note: A process is used in a scenario where there is no execution thread that shares data.) T hreads, on the other hand, are used in scenes where data is shared. B ecause Erlang does not share data between execution threads, we generally refer to it as a process. )
Erlang's built-in function spawn can be used to create a
spawn(Module, Exported_Function, List of Arguments)
S
uppose you have a module like this:
-module(tut14).
-export([start/0, say_something/2]).
say_something(What, 0) ->
done;
say_something(What, Times) ->
io:format("~p~n", [What]),
say_something(What, Times - 1).
start() ->
spawn(tut14, say_something, [hello, 3]),
spawn(tut14, say_something, [goodbye, 3]).
5> c(tut14).
{ok,tut14}
6> tut14:say_something(hello, 3).
hello
hello
hello
done
As shown above, say_something the function outputs the value of the first argument multiple times based on the number of times specified by the second argument. T he function starts two Erlang processes, one of which outputs "hello" three times and the other "goodbye" three times. T he function is called in say_something process. H owever, it is important to note that to start a process with a function, the function must export the module and start with spawn.
9> tut14:start().
hello
goodbye
<0.63.0>
hello
goodbye
hello
goodbye
Note that this is not the first time to output "goodbye" three times and then three times to output "goodbye". I
nstead, the first process outputs a "hello" and then the second process outputs "goodbye" again. N
ext, the first process outputs the second "hello". B
ut the
<0.63.0>
did it come from? I
n the Erlang system, the return value of a function is the value of the last expression of the function, and the last expression of the start function is:
spawn(tut14, say_something, [goodbye, 3]).
Spawn returns the identifier of the process, which is simply pid. A
process identifier is a tag that uniquely identifies an Erlang process. S
o,
<0.63.0>
process identifier returned by spawn. T
he following example shows how to use a process identifier.
In addition, in this
~p
io:format
is not
~w
T
he user manual is
~w
quoted as saying:
~p
data is output in the format of standard syntax,
~p
but when the output needs to take up multiple lines, the output can behave smarter at the branch." I
n addition, it attempts to detect the outputable string in the list and outputs by string."