`
lukeshei
  • 浏览: 388717 次
  • 性别: Icon_minigender_1
  • 来自: 台北
社区版块
存档分类
最新评论

Concurrent Programming II

阅读更多
1.Concurrency is easy…? <br /><br />( <a href="http://armstrongonsoftware.blogspot.com/2006/08/concurrency-is-easy.html">http://armstrongonsoftware.blogspot.com/2006/08/concurrency-is-easy.html</a> )<br /><br /><br />2.Sequential Programming<br /><br />-module(factorial).<br />-export([factoriala/1, factorialb/1, factorialc/1, factoriald/1]).<br /><br /><br />%Simplest:<br />factoriala(0) -> 1;<br />factoriala(N) -> N * factoriala(N - 1).<br /><br /><br />%Using function guards:<br />factorialb(0) -> 1;<br />factorialb(N) when N > 0 -> N * factorialb(N - 1).<br /><br /><br />%Using if:<br />factorialc(N) -><br /> if<br /> N == 0 -> 1;<br /> N > 0 -> N * factorialc(N - 1)<br />end.<br /><br /><br />%Using case:<br />factoriald(N) -><br /> case N of<br /> 0 -> 1;<br /> N when N > 0 -> N * factoriald(N - 1)<br />end.<br /><br /><br /><br />-module(set).<br />-export([new/0, add_element/2, del_element/2,is_element/2, is_empty/1, union/2, intersection/2]).<br /><br />new() -> [].<br /><br />is_element(H, [H|_]) -> true;<br />is_element(H, [_|Set]) -> is_element(H, Set);<br />is_element(_, []) -> false.<br /><br /><br />is_empty([]) -> true;<br />is_empty(_) -> false<br /><br />add_element(X, Set) -><br />case is_element(X, Set) of<br /> true -> Set;<br /> false -> [X|Set]<br />end.<br /><br /><br />del_element(X, [X|T]) -> T;<br />del_element(X, [Y|T]) -> [Y|del_element(X,T)];<br />del_element(_, []) -> [].<br /><br /><br />union([H|T], Set) -> union(T, add_element(H, Set));<br />union([], Set) -> Set.<br /><br /><br />intersection(S1, S2) -> intersection(S1, S2, []).<br />intersection([], _, S) -> S;<br />intersection([H|T], S1, S) -><br />case is_element(H,S1) of<br /> true -> intersection(T, S1, [H|S]);<br /> false -> intersection(T, S1, S)<br />end.<br /><br /><br />3.concurrent programming<br /><br />Processes and communication between processes are fundamental concepts in Erlang.<br /><br />1. Process Create<br /><br /> Pid = spawn(Module, FunctionName, ArgumentList)<br /><br /> 2. Inter-process Communication<br /><br /> Pid ! Message<br /><br /> receive<br /> Message1 [when Guard1] ->Actions1 ;<br /> Message2 [when Guard2] ->Actions2 ;<br /> _AnyMessage -> Actions3 ;<br /> after Timeout ->Action4 <br /> ...<br /> end.<br /> <br /> 3.Receiving messages from a specific process<br /> <br /> Pid ! {self(),abc}<br /><br /> receive<br /> {Pid,Msg} -><br /> ...<br /> end<br /> <br /><br />4. Examples<br /><br /> a)<br /> -module(cfac).<br /> -export([start/0,loop/0]).<br /> -import(factorial,[factoriala/1]).<br /><br /> start() ->spawn(counter, loop, []).<br /> <br /> loop() -><br /> receive<br /> {fac,Val}-> io:format("(from:~p)fac ~p=~p ~n",[node(),Val, <br /> factoriala(Val)]),loop();<br /> {exit}->io:format("end~n",[]);<br /> _->loop()<br /> end.<br /><br /><br /> b) performance testing<br /><br /> <br /> c) programming with socket <br /> -module(geturl).<br /> -export([start/2,senddata/1,dataarrive/1,startN/3]).<br /> -define(TCP_OPTIONS,[list, {packet, 0}, {active, false}, {reuseaddr, <br /> true}]).<br /><br /> start(Url,Port)-><br /> case gen_tcp:connect(Url,Port ,?TCP_OPTIONS ) of<br /> {ok,CSocket}->geturl:senddata(CSocket);<br /> {error,Why}->Why<br /> end.<br /> <br /> senddata(Socket)-><br /> Data="GET / HTTP/1.0\r\nConnection: close\r\nCache-Control: <br /> no-cache\r\n\r\n",<br /> io:format("~p~n",[Socket]),<br /> <br /> case gen_tcp:send(Socket,Data) of<br /> ok->io:format("send:~p~n",[Data]),dataarrive(Socket);<br /> {error, Why}->io:format("send_socket close:~p~n",[Why])<br /> end. <br /><br /> dataarrive(Socket)-> <br /> case gen_tcp:recv(Socket, 0) of<br /> { ok, Data} ->io:format("receive:~p~n",[Data]),<br /> dataarrive(Socket);<br /> {error, Why} ->io:format("dataarrive_socket <br /> close:~p~n",[Why])<br /> end.<br /><br /> startN(_,_,0)->io:format("~p~n",["end"]);<br /> startN(Url,Port,Num)->spawn(geturl,start,[Url,Port]), <br /> startN(Url,Port,Num-1).<br /><br /><br /> d) Distributes Programming<br /><br /> erl –sname local<br /> erl –sname remote<br /><br /> Pid=rpc:call(remote@se6,cfac,start,[]).<br /> Pid ! {fac,10}.<br /> <br /><br /><br />5.References<br /><br />[1] J. Armstrong, R. Virding, C. Wikstr¨om, and M. Williams. Concurrent Programming in Erlang.<br />[2] J. Armstrong.Erlang Programming.
分享到:
评论

相关推荐

    Windows并发编程指南 Concurrent Programming on Windows (english)part2

    Windows并发编程指南 Concurrent Programming on Windows (english)part2Joe Duffy是Microsoft公司.NET框架团队中并行扩展功能的技术主管、架构师以及奠基者,就职于Visual Studio部门。除了编写代码和管理开发团队...

    Concurrent Programming in Java Design Principles and Pattern

    Concurrent Programming in Java Design Principles and Pattern英文版 2.48M Java并发编程设计原则与模式_第二版(原书中文版) 19.4M Concurrent_Programming_in_Java_Design_Principles_Lecture DougLea

    Learning Concurrent Programming in Scala, 2nd Edition

    Title: Learning Concurrent Programming in Scala, 2nd Edition Author: Aleksandar Prokopec Length: 382 pages Edition: 2nd Revised edition Language: English Publisher: Packt Publishing - ebooks Account ...

    Learning Concurrent Programming in Scala mobi

    Learning Concurrent Programming in Scala 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Concurrent Programming on Windows 无水印pdf

    Concurrent Programming on Windows 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系...

    concurrent programming in java design principles and patterns .chm

    concurrent programming in java design principles and patterns .chm

    Learning Concurrent Programming in Scala, 2nd Edition.pdf

    Learning Concurrent Programming in Scala, 2nd Edition.pdf 就 有

    Doug Lea, Concurrent Programming in Java Design Principles and Patterns

    《Doug Lea, Concurrent Programming in Java Design Principles and Patterns》是一本深入探讨Java并发编程的经典著作,由Doug Lea撰写。这本书对于理解Java平台上的多线程编程和并发设计模式至关重要,是许多Java...

    Concurrent programming on windows

    #### 标题:并发编程在Windows(Concurrent programming on Windows) 并发编程在Windows这一主题主要聚焦于如何在Windows操作系统上有效地设计、实现和管理多线程或多进程的应用程序。随着多核处理器的普及和高...

    Concurrent Programming on Windows - Synchronization and Time

    overwhelmed with theory that isn't directly relevant to actual programming. I抳e structured the book into four major sections. The first, Concepts, introduces the reader to concurrency at a broad ...

    Concepts and Notations for Concurrent Programming

    《Concepts and Notations for Concurrent Programming》这篇论文由Gregory R. Andrews和Fred B. Schneider撰写,深入探讨了并行编程的核心概念和技术。尽管这是一篇较为古老的文章,但其内容仍然具有很高的参考价值...

    Concurrent Programming Without Locks

    ### 不使用锁的并发编程 #### 引言 在共享内存数据结构的并发控制中,互斥锁一直是事实上的标准机制。尽管它们看似简单,但设计可扩展的锁策略却非常困难,因为锁可能导致诸如优先级反转、死锁以及队列效应等问题...

    Concurrent Programming in Java

    Java的并发库提供了一系列工具和API,如`java.util.concurrent`包,帮助开发者有效地管理并发任务。本书主要涵盖以下几个方面: 1. **线程基础**:书中首先介绍了线程的基本概念,包括如何创建和管理线程,以及线程...

    Concurrent Programming on Windows

    《Windows并发编程指南》英文版,美中不足的是divu版本的,可能不如PDF来的方便,目录我也没有时间编辑。但是对阅读者来说,这是没有太大影响的。

    Learning Concurrent Programming in Scala

    《Learning Concurrent Programming in Scala》第二版是一本详尽介绍了如何使用Scala构建复杂的、可扩展的并发应用程序的书籍。本书通过实际案例和理论讲解相结合的方式,帮助读者掌握以下关键概念和技术: 1. **...

    concurrent programming in java(doug lea)

    《Java并发编程》一书是由著名并发编程专家Doug Lea所著,他同时也是Java并发包(JUC)的作者,这本书详细介绍了Java多线程编程的基础概念和高级技术。 首先,书中提到了并发编程的基本概念,包括并发模型、设计力...

    Concurrent Programming in Java™: Design Principles and Patterns, Second Edition

    Concurrent Programming in Java™: Design Principles and Patterns, Second Edition. 介绍并发编程的好的著作,著名的并发大师 Doug Lea的杰作。

    Windows并发编程指南 Concurrent Programming on Windows (english)

    他当前的研究领域包括:函数式编程(Functional Programming)、类型系统中的并发安全性,以及开发一些编程模型来充分利用GPU和SIMD等处理器。Joe还是并行Parallel LINQ(PLINQ)的开发者,以及在通用语言运行时...

    Concurrent Programming in Java™: Design Principles and Patterns 2nd

    本书《Concurrent Programming in Java™: Design Principles and Patterns 2nd》由Doug Lea编写,出版于1999年,是关于Java并发编程的一本权威指南。Java平台因其强大的线程支持能力而备受青睐,这使得Java程序员...

Global site tag (gtag.js) - Google Analytics