首页 技术 正文
技术 2022年11月10日
0 收藏 975 点赞 4,150 浏览 2881 个字

lua中的协程和线程类似:

  1. 协程拥有自己的独立的栈,局部变量,和指令;

  2. 所有协程都可以共享全局变量;

  3. 协程不能像线程那样并行执行,协程之间需要相互协调执行,同一个时刻只能运行一个协程;

如何使用协程:

  coroutine.create:创建一个协程,返回一个协程句柄;

  coroutine.status:查看一个协程的状态,suspended,running,dead,normal;

  coroutine.resume:恢复一个协程的执行,如果正常就返回true,错误返回false和一条错误信息;

  coroutine.yield:挂起一个协程的执行;

  resume-yield的数据返回:

  例子:

  local co2 = coroutine.create(function(a, b, c)
    return coroutine.yield(a + b, b + c, c + a)
  end)

  print(“resume”, coroutine.resume(co2, 1, 2, 3)) // 打印 resume true 3 5 4 yield时,返回yield的参数
  print(“resume”, coroutine.resume(co2, 1, 2, 3)) // 打印 resume true 1 2 3 resume时,返回resume的参数

生产者和消费者实例:

以消费者为驱动,即主循环在消费者,这里有两个消费者,同时向一个生产者获取数据。生产者每产生一个数据就会被挂起,知道下次被消费者激活。

协程程序一旦执行完成,协程状态就会被设置为dead,即死亡状态,如果试图用resume去激活一个死亡状态的协程会出现错误,可以通过判断resume的第一个返回判断协程运行是否正常。

local producer = coroutine.create(function()
local i = ; while true do
i = i + ;
--print(i) coroutine.yield(i)
end
end)function comsumer_func()
local status = data =
while true do
if (data >= ) then
break
end status, data = coroutine.resume(producer)
print("comsumer1:", data) coroutine.yield()
end
endlocal comsumer1 = coroutine.create(comsumer_func)
local comsumer2 = coroutine.create(comsumer_func)
local comsumers = coroutine.create(function(...)
local threads = {...}
local threads_status = {}
for i, v in ipairs(threads) do
threads_status[i] = true
end while true do
local status = false for i, v in ipairs(threads) do
if (threads_status[i]) then
threads_status[i] = coroutine.resume(threads[i])
end status = status or threads_status[i];
end if (not status) then
break
end
end
end)print(coroutine.resume(comsumers, comsumer1, comsumer2))print(coroutine.status(producer))
print(coroutine.status(comsumer1))
print(coroutine.status(comsumer2))

排列组合迭代器实例:

function generate(t, i, n)
if (i >= n) then
coroutine.yield(t)
else
for j = i, n do
t[j], t[i] = t[i], t[j];
generate(t, i+, n);
t[j], t[i] = t[i], t[j];
end
end
endfunction iter(t)
local co = coroutine.create(function()
generate(t, , #t)
end) return function()
local status, data = coroutine.resume(co)
return data
end
endlocal t = {"a", "b", "c", "e"}
for t in iter(t) do
for j = , #t do
io.write(t[j])
end
io.write("\n")
end

多线程下载文件实例:

require "socket"function http_get_file(host, file)
local con = assert(socket.connect(host, ))
con:settimeout()
con:send("GET "..file.." HTTP/1.0\r\n\r\n") print("start to download "..file) while true do
local data, status, partial = con:receive()
io.write(data or partial) if status == "timeout" then
--print("timeout")
coroutine.yield(con)
elseif status == "closed" then
print("closed")
break
end
end con:close()
endlocal download_threads = {}function create_download_task(host, file)
local thread = assert(coroutine.create(function()
http_get_file(host, file)
end)) print("thread", thread) table.insert(download_threads, thread)
endfunction start_to_download()
local i =
while true do
if (#download_threads == ) then break end if (download_threads[i]) then
local status, con = coroutine.resume(download_threads[i]) if not con then
table.remove(download_threads, i)
i = i -
end
end i = (i + )
if i > #download_threads then
i =
end
end
endcreate_download_task("news.zol.com.cn", "/591/5916908.html")
create_download_task("www.baidu.com", "/")start_to_download()
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,492
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,293