lu协程练习
生产者和消费者问题:
当协程调用yield时,从一个悬而未决的resume中返回。
简单的协程练习:
function receive()
local status,value = coroutine.resume(producer)
return status,value
end function send(x)
coroutine.yield(x)
end producer = coroutine.create(
function()
local x =
while true do
x = x+
if(x > ) then
break
end
send(x)
end
end) local status,res
repeat
status,res = receive()
print(status, res)
until(nil == res)
输出:
>lua -e "io.stdout:setvbuf 'no'" "1.lua"
true 1
true 2
true 3
true 4
true 5
true 6
true 7
true 8
true 9
true 10
true nil
>Exit code: 0
这种模式被称为消费者驱动模式。
过滤器filter是一种位于生产者和消费者之间的处理函数,用于对数据进行变换。它既是一个生产者又是消费者,他唤醒生产者产生new value,然后又将变换后的值传给消费者。
表示自己写的代码,出问题了:
function receive(prod)
local value = coroutine.resume(prod)
return value
end function send(x)
coroutine.yield(x)
end x =
function producer()
return coroutine.create(
function()
x = x +
print(sting.format("producer:%d.\n",x))
return x
end)
end function filter(prod)
return coroutine.create(
function()
local x = receive(prod)
x = string.format("Add:%d",x)
send(x)
end
)
end function consumer(prod)
repeat
res = receive(prod)
print(res)
until (nil == res)
end p = producer()
f = filter(p)
print(x)
consumer(f)
晚上回去看看怎么回事。
lu协程练习的更多相关文章
- C/C++使用Lu脚本协程
欢迎访问Lu程序设计 C/C++使用Lu脚本协程 1 说明 要演示本文的例子,你必须下载Lu32脚本系统.本文的例子需要lu32.dll.lu32.lib.C格式的头文件lu32.h,相信你会找到并正 ...
- tbox新增stackless协程支持
tbox之前提供的stackfull协程库,虽然切换效率已经非常高了,但是由于每个协程都需要维护一个独立的堆栈, 内存空间利用率不是很高,在并发量非常大的时候,内存使用量会相当大. 之前考虑过采用st ...
- tbox协程使用之切换与等待
tbox的协程实现,是stackfull模式的,需要指定独立堆栈和协程函数,目前暂时还不能像golang那样实现堆栈的动态增长,之后会对其进行支持. 目前提供下面一些功能特性: 1. 提供yield切 ...
- 记boost协程切换bug发现和分析
在分析了各大开源协程库实现后,最终选择参考boost.context的汇编实现,来写tbox的切换内核. 在这过程中,我对boost各个架构平台下的context切换,都进行了分析和测试. 在maco ...
- Python(八)进程、线程、协程篇
本章内容: 线程(线程锁.threading.Event.queue 队列.生产者消费者模型.自定义线程池) 进程(数据共享.进程池) 协程 线程 Threading用于提供线程相关的操作.线程是应用 ...
- Lua的协程和协程库详解
我们首先介绍一下什么是协程.然后详细介绍一下coroutine库,然后介绍一下协程的简单用法,最后介绍一下协程的复杂用法. 一.协程是什么? (1)线程 首先复习一下多线程.我们都知道线程——Thre ...
- 协程--gevent模块(单线程高并发)
先恶补一下知识点,上节回顾 上下文切换:当CPU从执行一个线程切换到执行另外一个线程的时候,它需要先存储当前线程的本地的数据,程序指针等,然后载入另一个线程的本地数据,程序指针等,最后才开始执行.这种 ...
- Python 【第五章】:线程、进程和协程
Python线程 Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. #!/usr/bin/env python # -*- coding:utf-8 -*- import t ...
- 进击的Python【第十章】:Python的socket高级应用(多进程,协程与异步)
Python的socket高级应用(多进程,协程与异步)
随机推荐
- 【大数据】下载Windows版本的Redis 转
https://www.cnblogs.com/tommy-huang/p/6093813.html 下载Windows版本的Redis 1.打开官网http://redis.io/点击Downl ...
- 转: H264码流分析 --264分析两大利器:264VISA和Elecard StreamEye Tools
转码: http://www.360doc.com/content/13/0225/19/21412_267854467.shtml ESEYE视频工具全称是什么: Elecard StreamEye ...
- You must have a copy of the scp binary locally to use the scp feature
在运行docker-machine scp 命令的时候,报错: "You must have a copy of the scp binary locally to use the scp ...
- gl.h included before glew.h
So I'm trying to move my OpenGL code from Main() into a specific class that will handle the 3D gra ...
- [Functional Programming] Using Last monoid with Maybe
Imaging we have a deck of cards, eveytimes we need to pick one card from deck, the result we want to ...
- Active Directory 域服务(AD DS)
本文内容 概述 工作组架构与域架构 名称空间(Namespace) 对象(Object).容器(Container)与组织单位(Organization Units,OU) 域树(Domain Tre ...
- SuperMap iDesktop之导入数据
SuperMap作为一个平台软件有自己的数据格式,现要将ESRI的SHP数据导入到SuperMap的udb数据库中,可以完成导入,但也不得不说几点问题. 下面是ArcGIS中批量导入SHP的操作界面. ...
- Yahoo邮箱最后登录,成为历史!
- JDK5.0 特性-线程任务执行架构 ScheduledExecutorService
来自:http://www.cnblogs.com/taven/archive/2011/12/17/2291469.html import java.util.concurrent.Callable ...
- string format 格式化小数位
String具体的格式化数据的方法 int a = 12345678;格式为sring输出Label1.Text = string.Format("asdfadsf{0}adsfasdf&q ...