Lua编程示例(八):生产者-消费者问题_Lua

这个问题是比较经典的啦,基本所有语言的多线程都会涉及到,但是没想到Lua的这个这么复杂 抓狂
  看了好长时间才算看明白,先上个逻辑图:

   开始时调用消费者,当消费者需要值时,再调用生产者生产值,生产者生产值后停止,直到消费者再次请求。设计为消费者驱动的设计。
   图画的不太好,可以先将Filter遮住,它是过滤器对两个程序之间传递的信息进行处理。去掉Filter逻辑就更清晰些了,就是两个“线程”(其实是两个协同程序)互相调用。resume回到yield处开始,支持嵌套,返回到栈顶的yield位置。yield是非阻塞的“线程同步”。这到有点像linux里的管道通信。

 

 function receive(prod)
 print("receive is called")
 local status,value = coroutine.resume(prod)
 return value
end

function send(x,prod)
 print("send is called")
 return coroutine.yield(x)
end

function producer()
 return coroutine.create(function ()
 print("producer is called")
 while true do
 print("producer run again")
  local x = io.read()
  send(x)
 end
 end)
end

function filter(prod)
 return coroutine.create(function ()
 for line = 1,1000 do
  print("enter fliter "..line)
  local x = receive(prod)
  print("receive in filter finished")
  x= string.format("%5d %s",line,x)
  send(x,prod)
 end
 end)
end

function consumer(prod)
 print("consumer is called")
 while true do
 print("consumer run again")
 local x = receive(prod)
 print("retrun customer")
 io.write(x,"\n")
 end
end

p = producer()
f=filter(p)
consumer(f)

运行结果:

consumer is called
consumer run again
receive is called
enter fliter 1
receive is called
producer is called
producer run again
fsy
send is called
receive in filter finished
send is called
retrun customer
  1 fsy
consumer run again
receive is called
enter fliter 2
receive is called
producer run again
gaga
send is called
receive in filter finished
send is called
retrun customer
  2 gaga
consumer run again
receive is called
enter fliter 3
receive is called
producer run again
......

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索lua
, 消费者
, 生产者
编程示例
,以便于您获取更多的相关知识。

时间: 2024-09-22 18:34:17

Lua编程示例(八):生产者-消费者问题_Lua的相关文章

Lua编程示例(二):面向对象、metatable对表进行扩展_Lua

counter = { count = 0 } function counter.get(self) return self.count end function counter:inc() self.count=self.count+1 end print(counter.get(counter)) counter.inc(counter) print(counter.get(counter)) counter2={ count=4, get = counter.get, inc = coun

Lua编程示例(七):协同程序基础逻辑_Lua

co=coroutine.create(function() print("hi") end) print(coroutine.status(co)) coroutine.resume(co) print(coroutine.status(co)) print() co=coroutine.create(function() for i=1,2 do print("co",i) coroutine.yield() end end) coroutine.resume(

Lua编程示例(四):Lua标准库之表库、字符串库、系统库_Lua

tb1 = { "alpha","log","gamme"} print(table.concat(tb1," , ")) print(table.concat(tb1,"\n",nil,2)) print() tb1[88.99] = 'aaa' --返回索引值最大的值,并且计算小数 print(table.maxn(tb1)) print() --默认删除索引最大的元素并返回 print(table.r

Lua编程示例(三):稀疏表、双端队列、格式化输出、表和循环表的格式化输出_Lua

a={} for i=1,10 do a[i]={} for j=0,10 do if(i%2==0) then a[i][j]=0 end end end print(a[9][10]) print(a[10][10]) print() --双端队列 List={} function List.new() return {first = 0,last = -1} end function List.pushleft(list,value) local first= list.first-1 l

Lua编程示例(五): C语言对Lua表的读取和添加_Lua

#include "stdafx.h" lua_State *L; void load_lua(char *filename){ L=luaL_newstate(); luaL_openlibs(L); if((luaL_loadfile(L,filename) || lua_pcall(L,0,0,0))!= 0){ luaL_error(L,"loadfile error! \n %s",lua_tostring(L,-1)); } } double getfi

Lua编程示例(六): C语言调用Lua函数_Lua

C++端: #include "stdafx.h" lua_State *L; void load_lua(lua_State **L,char *filename){ *L=luaL_newstate(); luaL_openlibs(*L); if(luaL_loadfile(*L,filename) || lua_pcall(*L,0,0,0)){ luaL_error(*L,"load file error! %s",lua_tostring(*L,-1))

Java并发编程示例(八):处理线程的非受检异常_java

Java语言中,把异常分为两类: 受检异常: 这类异常必须在throws子句中被显式抛出或者在方法内被捕获.例如,IOException异常或ClassNotFoundException异常.非受检异常: 这类异常不需要显式抛出或捕获.例如,NumberFormatException异常. 当一个受检异常在Thread对象的run()方法中被抛出时,我们必须捕获并处理它,因为run()方法不能抛出异常.而一个非受检异常在Thread对象的run()方法中被抛出时,默认的行为是在控制台打印出堆栈跟

Lua编程示例(一):select、debug、可变参数、table操作、error_Lua

function test_print(...) for i=1,select("#",...) do print(i,select(i,...)) end end test_print(11,12,13,14) print() print(debug.traceback()) print() function test(...) for i=1,arg.n do print(i.."\t"..arg[i]) end end test("a",2

用Python多线程实现生产者消费者模式

什么是生产者消费者模式 在软件开发的过程中,经常碰到这样的场景: 某些模块负责生产数据,这些数据由其他模块来负责处理(此处的模块可能是:函数.线程.进程等).产生数据的模块称为生产者,而处理数据的模块称为消费者.在生产者与消费者之间的缓冲区称之为仓库.生产者负责往仓库运输商品,而消费者负责从仓库里取出商品,这就构成了生产者消费者模式. 结构图如下: 为了大家容易理解,我们举一个寄信的例子.假设你要寄一封信,大致过程如下: 你把信写好--相当于生产者生产数据 你把信放入邮箱--相当于生产者把数据放