See Also: http://www.erlang.org/course/concurrent_programming.html
Definitions
Creating a new process
Simple message passing
An Echo Process
Selective Message Reception
Selection of Any Message
A Telephony Example
Pids can be sent in messages
Registered Processes
The Client Server Model
Timeouts
Definitions定义
Process 轻量级进程,系统会有许多同时运行的process Message 进程间通过消息通讯 Timeout 超时机制 Registered Process 注册了名称的进程 Client/Server Model
Creating a new process创建一个新进程
Pid1进程中执行如下代码 Pid2 = spawn(Mod,Func,Args) Pid2是新进程的标识,只有Pid1知道
Simple Message Passing消息传递
%mesTest.erl -module(mesTest). -export([recvTest/0]). recvTest() -> receive finished -> io:format("program stopped!~n",[]); {From,Msg} -> io:format("Received ~p ~p~n", [From,Msg]) end. 1> c(mesTest). {ok,mesTest} 2> Pid2 = spawn(mesTest,recvTest,[]). <0.39.0> 3> Pid2!{self(),"Hello Pid2"}. Receoved <0.32.0> "Hello Pid2" {<0.32.0>,"Hello Pid2"} 4> Pid2!{self(),"Hello Pid2"}. %Pid2进程已经结束 {<0.32.0>,"Hello Pid2"} 5>
self() 返回自身进程的pid
From 和 Msg 匹配收到的消息 (也可以调用Pid2!finished)
如果From之前已经被bound数据,那只有Msg会收到内容
An Echo Process
% echo.erl -module(echo). -export([go/0, loop/0]). go() -> Pid2 = spawn(echo, loop, []), Pid2 ! {self(), hello}, receive {Pid2, Msg} -> io:format("P1 ~w~n",[Msg]) end, Pid2 ! stop. loop() -> receive {From, Msg} -> From ! {self(), Msg}, loop(); stop -> true end. 执行echo:go().
Selective Message Reception
% echo.erl -module(echo). -export([test/0,selective/0,bfunc/1]). selective() -> receive foo -> io:format("Received foo from A~n",[]) end, receive bar -> io:format("Received bar from B~n",[]) end. bfunc(Cpid) -> Cpid ! bar. test() -> Cpid = spawn(echo, selective, []), spawn(echo, bfunc, [Cpid]), timer:sleep(1000), Cpid ! foo. 1> c(echo). {ok,echo} 2> echo:test(). Received foo from A foo Received bar from B
Selection of Any Message
-module(echo). -export([test/0,selective/0,bfunc/1]). selective() -> receive Msg -> io:format("Received ~w ~n",[Msg]) end, receive Msg2 -> io:format("Received ~w ~n",[Msg2]) end. bfunc(Cpid) -> Cpid ! bar. test() -> Cpid = spawn(echo, selective, []), spawn(echo, bfunc, [Cpid]), timer:sleep(1000), Cpid ! foo. 1> c(echo). {ok,echo} 2> echo:test(). Received bar Received foo foo
A Telephony Example
%这个例子没能理解
Pids can be sent in messages
% A 把自己的Pid发送给B,B再将A的pid给C,最终C直接向A回话 % echo.erl -module(echo). -export([test/0,bfunc/0,cfunc/0]). cfunc() -> receive {_,Apid} -> Apid ! foo end. bfunc() -> receive {Cpid,Apid} -> Cpid ! {"Call A",Apid} end. test() -> %create b and c process Bpid = spawn(echo,bfunc,[]), Cpid = spawn(echo,cfunc,[]), Bpid ! {Cpid,self()}, receive Mes -> io:format("Received ~w from C ~n",[Mes]) end. 1> c(echo). {ok,echo} 2> echo:test(). Received foo from C ok
Registered Processes
register(Alias, Pid) Registers the process Pid with the name Alias.
% echo.erl -module(echo). -export([test/0,bfunc/0,cfunc/0]). cfunc() -> receive {_,Apid} -> Apid ! foo end. bfunc() -> receive {_,Apid} -> cpid ! {"Call A",Apid} end. test() -> %create b and c process Bpid = spawn(echo,bfunc,[]), Cpid = spawn(echo,cfunc,[]), register(cpid,Cpid), %BPid可以通过cpid直接向C发送消息了 Bpid ! {Cpid,self()}, receive Mes -> io:format("Received ~w from C ~n",[Mes]) end.
Client Server Model
% myserver.erl -module(myserver). -export([start/0,server/0,request/1]). handle(Reqdata) -> "Hello " ++ Reqdata. server() -> io:format("server id = ~w ~n",[self()]), receive [From,{request,X}] -> Resp = handle(X), From ! {reply,Resp}, server() end. start() -> register(serverid,spawn(myserver,server,[])). request(Name) -> serverid ! [self(),{request,Name}], receive {reply,Res} -> io:format("Get ~p from server!",[Res]) after 5000 -> io:format("No response from server~n",[]), exit(timeout) end. 1> c(myserver). {ok,myserver} 2> myserver:start(). server id = <0.39.0> true 3> myserver:request("PJ"). server id = <0.39.0> Get "Hello PJ" from server!ok 4>
Uses of Timeouts
相关推荐
Erlang并发编程,Erlang程序设计,Erlang中文手册。 学习erlang的好资料。 Erlang是一个结构化,动态类型编程语言,内建并行计算支持。最初是由爱立信专门为通信应用设计的,比如控制交换机或者变换协议等,因此...
二十多年来,在传统电信领域高并发、高可靠、高容错的严酷环境下,Erlang语言和OTP平台被锻炼得坚如磐石,浓郁的函数式特质更是恰到好处地弥补了传统命令式语言在并发编程上的固有缺陷,大大降低了构筑并发、容错、...
erlang 程序设计 源码 erlang 程序设计 源码 erlang 程序设计 源码erlang 程序设计 源码
书中兼顾了顺序编程、并发编程和分布式编程,重点介绍如何编写并发和分布式的Erlang程序以及如何在多核CPU上自动加速程序,并深入地讨论了开发Erlang应用中至关重要的文件和网络编程、OTP、ETS和DETS等主题。...
Erlang程序设计(第2版).pdf 高清带目录。 .
erlang程序设计 erlang入门手册
erlang程序设计第二版习题答案 ,是我自己写的习题解答,也对照过网上的一些答案,相对来说是更加简洁和符合题意的解答,不过后面几章因为时间问题没有写完,有问题或者意见可以私信找我。
Erlang程序设计 第2版 Erlang程序设计 第2版Erlang程序设计 第2版
[Erlang程序设计]源代码,包括了erlang程序设计一书全部实例代码;
Erlang程序设计,包含完整目录和全套源码
erlang编程指南学习笔记,包含具体操作命令例子
Erlang程序设计中文版 完整书签 erlang programming
Erlang/OTP并发编程实战 英文
erlang otp学习文档 学习还不错的
NULL 博文链接:https://jias.iteye.com/blog/615679
Erlang OTP并发编程实战高清非扫描版,基于一个simple cache深入浅出讲解erlang otp的使用。
《erlang/otp并发编程实战》侧重生产环境下的erlang 开发,主要讲解如何构建稳定、版本控制良好、可维护的产品级代码,凝聚了三位erlang 大师多年的实战经验。 《erlang/otp并发编程实战》主要分为三大部分:第一...
Erlang程序设计入门经典,中文高清版,随书光盘源码