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
resty thread
原创xiangjie256 ©著作权
©著作权归作者所有:来自51CTO博客作者xiangjie256的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Java Thread Dump文件分析
Java Thread Dump文件分析
java 堆栈 Java -
Golang:使用go-resty/resty发送http请求get和post
简单的 HTTP 和 REST 客户端,受到 Ruby rest-client 的启发。2、发送POST请求。
golang http 开发语言 github json -
resty redis resty redis cluster
resty-redis-cluster下载地址 https://github.com/steve0511/resty-redis-cluster
resty redis redis github 下载地址