`
DiaoCow
  • 浏览: 242804 次
  • 性别: 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)代码实现

    kmp游戏中使用频率较高,所以用erlang代码代码实现,效率还行。

    erlang代码热替换与应用部署

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

    erlang并发编程实战源代码

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

    [Erlang程序设计]源代码

    **Erlang程序设计源代码详解** Erlang是一种面向并发、函数式编程语言,尤其在分布式系统和高可用性领域表现出色。本资源包含了《Erlang程序设计》一书的所有实例代码,旨在帮助读者深入理解Erlang语言的核心特性和...

    Erlang程序设计].源代码

    这些源代码可能是书中实例的实现,或者是针对Erlang编程技巧和概念的示例。通过阅读和分析这些源代码,你可以深入理解Erlang的关键特性: 1. 函数式编程: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程序设计第2版附书代码

    在深入理解Erlang程序设计第2版的附书代码之前,我们先来了解一下Erlang的基本概念和特性。 1. **并发与并行**:Erlang的并发模型基于轻量级进程(Lightweight Processes, LWP),这些进程具有独立的执行路径,可以...

    erlang_版本24.3.4.4

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

    Erlang程序设计及源代码打包

    5. **热代码替换**:Erlang允许在运行时更新代码,无需停止服务,这对于维护和升级系统非常有利。 **随书源代码分析** `jaerlang-code.zip`文件包含了书中示例代码,这些代码可以帮助读者更好地理解书中的概念。...

    Erlang程序设计,包含完整目录和全套源码

    4. **进程间通信(IPC)**:通过`send`和`receive`操作,Erlang进程之间可以发送和接收消息,这是并发编程的基础。 5. **模块和函数**:Erlang的模块化设计有助于组织代码,函数是代码的基本执行单元。 6. **错误...

    erlang25.0 windows版本

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

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

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

    erlang资源

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

    erlang otp in action 书上的 代码

    erlang otp in action 代码

    Erlang程序设计及源码

    函数式编程的特性使Erlang代码更易于理解和调试,因为它们通常没有副作用,这在并行计算中尤为重要。 3. **错误处理与容错** Erlang采用“let it crash”哲学,即系统中出现问题时,允许错误的进程崩溃,然后由...

Global site tag (gtag.js) - Google Analytics