lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


This code will cause crash, in lua5.4.3 complied in windows10 msvc2019(x64).

And run in this site: Lua: demo result “your program was aborted”.

 

local coroutine = coroutine

local table = table

local traceback = debug.traceback

local function queue()

    local current_thread

    local ref = 0

    local thread_queue = {}

 

    local function xpcall_ret(ok, ...)

       ref = ref - 1

       if ref == 0 then

           current_thread = table.remove(thread_queue,1)

           if current_thread then

              local ok, err = coroutine.resume(current_thread)

              if not ok then

                  err = traceback(current_thread,tostring(err))

                  coroutine.close(current_thread)

                  error(err)

              end

           end

       end

       assert(ok, (...))

       return ...

    end

 

    return function(f, ...)

       local thread = coroutine.running()

       if current_thread and current_thread ~= thread then

           table.insert(thread_queue, thread)

           coroutine.yield()

           assert(ref == 0)  -- current_thread == thread

       end

       current_thread = thread

 

       ref = ref + 1

       return xpcall_ret(xpcall(f, traceback, ...))

    end

end

 

local lock = queue()

local tb = {}

 

for i=1,1000 do

    local co = coroutine.create(function(v)

       print("lock", i)

       lock(function()

           if 1==v then

              coroutine.yield(v)

           end

       end)

       print("unlock", i)

    end)

    tb[#tb+1] = co

    coroutine.resume(co,i )

end

 

coroutine.resume( tb[1] )

print("done")