并发原语
% 创建一个新的进程
Pid = spawn(Fun).
% 向进程发送消息
Pid ! Message.
% 接收一个发送给当前进程的消息
receive ... and.
一个简单的例子
%% area_server.erl
-module(area_server).
-export([loop/0]).
loop() ->
receive
{rectangle, Width, Ht} ->
io:format("Area of rectangle is ~p~n",[Width * Ht]),
loop();
{circle, R} ->
io:format("Area of circle is ~p~n", [3.14159 * R * R]),
loop();
Other ->
io:format("I don't know what the area of a ~p is ~n",[Other]),
loop()
end.
%% cmd
1> c(area_server).
{ok,area_server}
2> Pid = spawn(fun area_server:loop/0).
<0.66.0>
3> Pid ! {rectangle,6,10}.
Area of rectangle is 60
{rectangle,6,10}
4> Pid ! {circle,23}.
Area of circle is 1661.90111
{circle,23}
5> Pid ! {triangle,2,4,5}.
I don't know what the area of a {triangle,2,4,5} is
{triangle,2,4,5}
客户端服务器介绍
%% area_server_final.erl
-module(area_server_final).
-export([start/0, area/2]).
start() -> spawn(fun loop/0).
area(Pid, What) ->
rpc(Pid, What).
rpc(Pid, Request) ->
Pid ! {self(), Request},
receive
{Pid, Response} ->
Response
end.
loop() ->
receive
{From, {rectangle, Width, Ht}} ->
From ! {self(), Width * Ht},
loop();
{From, {circle, R}} ->
From ! {self(), 3.14159 * R * R},
loop();
{From, Other} ->
From ! {self(), {error,Other}},
loop()
end.
%% cmd
1> c(area_server_final).
{ok,area_server_final}
2> Pid = area_server_final:start().
<0.66.0>
3> area_server_final:area(Pid,{rectangle,10,8}).
80
4> area_server_final:area(Pid,{circle,4}).
50.26544
创建一个进程需要花费多少时间
%% processes.erl
-module(processes).
-export([max/1]).
max(N) ->
Max = erlang:system_info(process_limit),
io:format("Maximum allowed processes:~p~n",[Max]),
statistics(runtime),
statistics(wall_clock),
L = for(1, N, fun() -> spawn(fun() -> wait() end) end),
{_, Time1} = statistics(runtime),
{_, Time2} = statistics(wall_clock),
lists:foreach(fun(Pid) -> Pid ! die end, L),
U1 = Time1 * 1000 / N,
U2 = Time2 * 1000 / N,
io:format("Process spawn time=~p (~p) microseconds~n",
[U1, U2]).
wait() ->
receive
die -> void
end.
for(N, N, F) -> [F()];
for(I, N, F) -> [F()|for(I+1, N, F)].
%% cmd
1> c(processes).
{ok,processes}
2> processes:max(20000).
Maximum allowed processes:262144
Process spawn time=2.5 (4.4) microseconds
ok
%% cmd
% 设置进程数创建上限为 50000
# erl -P 50000
1> processes:max(50000).
Maximum allowed processes:262144
Process spawn time=1.4 (2.08) microseconds
ok
带超时的 receive
%% stimer.erl
-module(stimer).
-export([start/2, cancel/1]).
start(Time, Fun) -> spawn(fun() -> timer(Time, Fun) end).
cancel(Pid) -> Pid ! cancel.
timer(Time, Fun) ->
receive
cancel ->
void
after Time ->
Fun()
end.
%% cmd
1> c(stimer).
{ok,stimer}
2> Pid = stimer:start(5000, fun()->io:format("timer event~n")end).
<0.66.0>
timer event
3> Pid1 = stimer:start(25000, fun()->io:format("timer event~n")end).
<0.68.0>
4> stimer:cancel(Pid1).
cancel
注册进程
%% clock.erl
-module(clock).
-export([start/2, stop/0]).
start(Time, Fun) ->
register(clock, spawn(fun() -> tick(Time, Fun) end)).
stop() -> clock ! stop.
tick(Time, Fun) ->
receive
stop ->
void
after Time ->
Fun(),
tick(Time, Fun)
end.
%% cmd
1> c(clock).
{ok,clock}
2> clock:start(5000, fun()->io:format("TICK ~p~n",[erlang:now()])end).
true
3> TICK {1508,672936,54000}
3> TICK {1508,672941,85000}
3> TICK {1508,672946,85000}
3> clock:stop().
stop
作者 Github : tojohnonly , 博客 : EnskDeCode