Erlang 的 spawn() 使用时,值得注意的一点
来源:互联网 发布:java 线程池 名字关闭 编辑:程序博客网 时间:2024/06/18 10:43
spawn() 在使用的过程中遇到一个有趣的事儿,描述如下:
A: spawn(fun() -> io:format("hello world~n", []) end),
B: spawn(out_put()).
以上两种方式的处理方式不同。
区别在于: A 直接返回 进程ID 而B 知道 out_put()结束才返回进程ID。
实例:
情况A:
-module(spawn_test).
-export([start/0]).
-export([out_time/1]).
out_time(0) ->
io:format("GAME OVER~n", []);
out_time(N) ->
receive after
1000 ->
io:format("the time is ~p~n", [erlang:now()])
end,
out_time(N - 1).
start() ->
io:format("start to run:~n", []),
Ret = spawn(fun() -> out_time(10) end),
io:format("return_value is ~p I am return, I am function.Just now is ~p~n", [Ret, erlang:now()]),
spawn(fun() -> io:format("I am fun, I am running.You are wrong.~n", []) end).
结果是:
![](file:///C:/Users/algoblu/AppData/Local/YNote/data/whc_forwardic@163.com/2bb5dc562ef944c993fddbefe9d678fa/clipboard.png)
情况B:
-module(spawn_test).
-export([start/0]).
-export([out_time/1]).
out_time(0) ->
io:format("GAME OVER~n", []);
out_time(N) ->
receive after
1000 ->
io:format("the time is ~p~n", [erlang:now()])
end,
out_time(N - 1).
start() ->
io:format("start to run:~n", []),
Ret = spawn(?MODULE, out_time, [10]),
io:format("return_value is ~p I am return, I am function.Just now is ~p~n", [Ret, erlang:now()]),
spawn(fun() -> io:format("I am fun, I am running.You are wrong.~n", []) end).
![](file:///C:/Users/algoblu/AppData/Local/YNote/data/whc_forwardic@163.com/c2a553975a2d4f01a57f3864112398bf/clipboard.png)
0 0
- Erlang 的 spawn() 使用时,值得注意的一点
- 76个值得你注意的erlang编程习惯【转】
- java中使用try-catch-finally一点值得注意的事
- 使用stringstream时需要注意的一点
- 对于VC++中的头文件包含值得注意的一点
- 游戏服务器开发中的一点值得注意的地方
- 关于数字运算符值得注意的一点小问题
- MSDN使用的一点注意
- jsp使用时一些值得注意的事项
- 使用XML值得注意的几个地方
- erlang 并发编程,spawn 创建新的进程
- 值得注意的地方
- 值得注意的append
- delphi中使用API时的一点注意之处
- delphi中使用API时的一点注意之处
- 使用HttpSendRequest函数POST数据时应该注意的一点
- 使用RegSetValueEx时要注意的一点问题
- 使用ValidationSummary控件时设置ShowMessageBox要注意的一点
- Android系统更新防互刷功能实现与分析
- 删除数据库中重复数据的几个方法
- 黑马程序员——关于IO流
- AngularJS
- 寻找最大数--贪心
- Erlang 的 spawn() 使用时,值得注意的一点
- 【动态加边spfa】[Noi2014] bzoj3669 魔法森林 forest
- 学习OpenCV范例(二十一)——Keypoints+Knn+findHomography进行目标定位
- Spark性能超过Hadoop百倍
- 第三周:程序阅读(1)
- 【Linux】为什么linux要用swap分区?
- 2015-03-19-数据规整化(3)-数据转换
- 彻底搞定C指针---指向指针的指针
- 编写一个函数,从一个字符串中提取一个子字符串