`
DiaoCow
  • 浏览: 246197 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Erlang receive代码块

阅读更多
receive代码块是如何执行的呢?



process会尝试从它的mail-box中循环取出消息进行pattern match:

1.若某一条消息pattern match成功,该条消息将从mail-box中移除并且执行相应的Expression,然后退出receive代码块(先前未匹配的消息,会重新放入mailbox)

2.若没有一条消息能够pattern match成功(或者mail-box中根本没有消息),则process阻塞在receive代码块(等待新消息),若在Time的时间范围内,依然没有消息能够pattern match,就执行超时处理TimeOutExpr,最后退出receive代码块

after 1000:     
表示若在1s中内没有消息能够pattern match ,则进行超时处理
after 0:            
表示若mail-box中没有消息能够pattern match ,则立马进行超时处理
after infinity:
表示若mail-box中没有消息能够pattern match ,则一直阻塞,直到有一条消息能够pattern match(这种情况等价于不写after,是receive代码块的默认操作)

-------------------------------------------------------------------------华丽分割线-------------------------------------------------------------------------

<<Programming Erlang>>相关原文:
receive
     Pattern1 [when Guard1] ->
               Expressions1;
     Pattern2 [when Guard1] ->
               Expressions1;
     ...
after
     Time ->
          ExpressionTimeout
end

receive works as follows:

1. When we enter a receive statement, we start a timer (but only if an after section is present in the expression).

2. Take the first message in the mailbox and try to match it against Pattern1, Pattern2, and so on. If the match succeeds, the message is removed from the mailbox, and the expressions following the pattern are evaluated.

3. If none of the patterns in the receive statement matches the first message in the mailbox, then the first message is removed from the mailbox and put into a “save queue.” The second message in the mailbox is then tried. This procedure is repeated until a matching message is found or until all the messages in the mail-box have been examined.

4. If none of the messages in the mailbox matches, then the process is suspended and will be rescheduled for execution the next time a new message is put in the mailbox. Note that when a new message arrives, the messages in the save queue are not rematched; only the new message is matched.

5. As soon as a message has been matched, then all messages that have been put into the save queue are reentered into the mailbox in the order in which they arrived at the process. If a timer was set, it is cleared.

6. If the timer elapses when we are waiting for a message, then evaluate the expressions ExpressionsTimeout and put any saved messages back into the mailbox in the order in which they arrived at the process.


  • 大小: 4.9 KB
分享到:
评论

相关推荐

    远古封神+英雄远征的ERLANG游戏服务器代码

    《远古封神》与《英雄远征》是两款受欢迎的网络游戏,它们的后端服务器采用了ERLANG这一编程语言来构建。ERLANG是一种为并发、分布式和容错系统设计的函数式编程语言,因其在实时系统和大规模并发处理中的优秀性能而...

    KMP(Erlang)代码实现

    由于部分内容中存在OCR扫描错误,但整体上代码逻辑清晰,我们能够理解其算法实现的要点,以及如何利用Erlang的特性来编写高效的字符串匹配算法。这种语言的函数式编程风格使得代码具有很好的可读性和易理解性。在...

    erlang代码热替换与应用部署

    本文将深入探讨Erlang中的代码热替换(Code Replacement)技术及其在应用部署中的重要性。 代码热替换是Erlang的一大特色,它允许在不中断运行服务的情况下更新和替换正在运行的代码。这一特性使得Elang系统可以在...

    erlang并发编程实战源代码

    erlang并发编程实战源代码erlang并发编程实战源代码

    erlang源码包

    例如,`tar -zxvf otp_src_R16B02.tar.gz`将解压出源代码目录。 3. **编译源码**:进入解压后的源码目录,执行配置脚本`./configure`来检测系统环境并准备编译。接着,使用`make`命令进行编译,这将生成Erlang的可...

    erlang编程 Introducing Erlang

    进程可以通过`send`操作向其他进程发送消息,并通过`receive`来接收。这种异步通信方式提供了非阻塞的执行环境,有利于提高系统的效率。 ### 6. 分布式特性 Erlang支持跨节点的分布式计算,允许不同物理机器上的...

    改进erlang版的protobuf代码

    标题中的“改进erlang版的protobuf代码”指的是在Erlang编程语言中对Protocol Buffers(protobuf)进行了优化和改进的代码实现。Protocol Buffers是一种数据序列化协议,由Google开发,它允许开发者定义数据结构,...

    二郎助手erlang开发工具、erlang编辑器

    二郎助手的核心特性在于其全面的Erlang支持,包括语法高亮、代码自动完成、错误检查和调试功能。作为一个基于VS2005开发的项目,它继承了Visual Studio家族的强大编辑器功能,并且针对Erlang语言进行了优化。这意味...

    erlang写的一个特别的web服务器

    emongrel2的源代码仓库(如GitHub)可能包含详细的README文件和示例,帮助开发者理解和使用这个服务器。 总的来说,emongrel2是Erlang对Mongrel2的实现,它结合了Erlang的并发优势和Mongrel2的设计理念,旨在构建一...

    erlang_版本24.3.4.4

    - **Pattern Matching**:Erlang的匹配操作符`=`允许在函数调用、case语句和receive语句中进行模式匹配。 熟悉这些概念和Erlang的编程范式后,你就可以开始构建高效、容错的并发应用程序了。无论是网络服务器、实时...

    Erlang实战项目源代码_tutorial.zip

    给定的压缩包文件名为"Erlang实战项目源代码_tutorial.zip",这表明该压缩包内含有一个Erlang的项目教程。项目教程一般会包括一系列的示例代码和指导性文档,用以帮助学习者逐步了解并掌握Erlang语言的实际应用。...

    erlang25.0 windows版本

    作为“源码软件”,Erlang 25.0同样提供了源代码,开发者可以深入研究其内部工作原理,进行定制化开发,或者为Erlang社区贡献代码。对于开发者而言,理解Erlang的源码可以帮助他们更好地利用这个平台,实现更高效、...

    erlang入门级练习:LeetCode OJ问题的部分erlang 源码

    我自己在新学erlang,在LeetCode OJ上找了题目练习,题目很适合新手熟悉语言,但是LeetCode OJ里面只有几门主流语言的答案,下面是已完成的erlang源代码,后续有空再做其他问题续传,题目包含:(源码开头都有题目...

    erlang趣学指南

    不过,理解Erlang的类型系统对于编写高效且可维护的代码至关重要。 递归在Erlang程序设计中占有重要地位,因为Erlang不支持传统的循环结构。理解如何有效地使用递归来解决问题是学习Erlang的一个关键点。 错误和...

    erlang资源

    5. **REPL(Read-Eval-Print Loop)**:Erlang shell的使用,它是学习和测试Erlang代码的重要工具。 6. **实际应用案例**:可能包含Erlang在实时系统、网络通信、数据库或Web服务等领域的应用示例。 这两本书结合...

    erlang otp in action 书上的 代码

    erlang otp in action 代码

    Erlang应用部署与热代码替换--理解2

    本篇将深入探讨Erlang应用的部署与热代码替换。 一、Erlang应用部署 在Erlang环境中,应用通常被打包成一个`.app`文件,包含应用元数据,以及一个或多个beam文件(编译后的Erlang代码)。部署Erlang应用的步骤如下...

    erlang22最新下载包

    在“erlang22最新下载包”中,提供的文件是`otp_src_22.1`,这表明这是一个源代码包,包含Erlang/OTP(开放电信平台)22.1版本的所有源代码。OTP是Erlang的标准化开发框架,提供了许多用于构建可靠和可维护的系统的...

    erlang programming

    6. **热升级与无中断服务**:Erlang支持代码热升级,即在不中断运行的情况下更新应用程序代码,这对于保持高可用性和服务连续性至关重要。 7. **并发性能优化**:Erlang的并发性能优化涉及进程数量的控制、消息队列...

    Erlang_CNode用户指

    4. **进程通信**:阐述如何在CNode中创建Erlang进程,以及如何使用send和receive原语进行消息传递。这通常涉及理解Erlang的消息队列模型和同步原语。 5. **错误处理和调试**:讨论在CNode中处理Erlang错误和异常的...

Global site tag (gtag.js) - Google Analytics