resty thread


local t = {}

local function produce_thread()
for i=1,5 do
t[i] = i
ngx.say("produce:",i)
ngx.sleep(0.01)
end
end

local function consume_thread()
for i=1,5 do
for k,v in pairs(t) do
if t[k] ~= 0 then
ngx.say("consume:",t[k])
t[k] = 0
end
end
ngx.sleep(0.03)
end
end

local tp, err = ngx.thread.spawn(produce_thread)
if not tp then
ngx.say("failed to create produce thread: ", err)
return
end

local tc, err = ngx.thread.spawn(consume_thread)
if not tc then
ngx.say("failed to create consume thread: ", err)
return
end

local ok, res = ngx.thread.wait(tp, tc)
if not ok then
ngx.say("failed to wait: ", res)
return
end

produce:1
consume:1
produce:2
produce:3
consume:2
consume:3
produce:4
produce:5
consume:4
consume:5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值