skynet queue

由于skynet服务有重入问题,所以找到了一个skynet queue方法。 

简单用法,假设test为在服务中需要顺序执行的函数。只要把一个函数往queue扔就行了。

local function test()
    print("hello world")
end

local queue = require "skynet.queue"
queue(test)

翻了一下源代码,实现是用一个闭包函数达到目的。整体思路就是记录下current thread,判断current thread是否和local thread相等,只有两者相等才执行。

以下是源码片段:

function skynet.queue()
        local current_thread
        local ref = 0
        local thread_queue = {}
        return function(f, ...)
                local thread = coroutine.running()
                if current_thread and current_thread ~= thread then
                        table.insert(thread_queue, thread)
                        skynet.wait()
                        assert(ref == 0)        -- current_thread == thread
                end
                current_thread = thread

                ref = ref + 1
                local ok, err = xpcall(f, traceback, ...)
                ref = ref - 1
                if ref == 0 then
                        current_thread = table.remove(thread_queue,1)
                        if current_thread then
                                skynet.wakeup(current_thread)
                        end
                end
                assert(ok,err)
        end
end

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值