According to the documentation, coroutine should run the subroutine in a separate thread, but this does not happen:

function test() while true do --nothing end end co = coroutine.create(test) print("STARTED") coroutine.resume(co) print("FINISHED") 

I expect to get on the output of the line "STARTED" and "FINISHED", and I get only "STARTED"! The test function, launched via coroutine, continues to block the general thread.

    1 answer 1

    coroutine is a coroutine, not a flow, which is written in the doc. They work in the same thread, and transfer control to each other independently. A small example:

     function test(...) print(...) print(coroutine.yield('First return')) return 'Second (real) retrn' end co = coroutine.create(test) print("STARTED") print(coroutine.resume(co, 'Argument to first call')) print(coroutine.resume(co, 'Argument to second call')) print("FINISHED") 

    He is typing

     STARTED Argument to first call true First return Argument to second call true Second (real) retrn FINISHED 
    • Is there a way to run a function in a separate thread, just as it is implemented in javascript? I am writing a simple udp server for a small love game and I would not want to mess around with heavy frameworks, I just need to somehow handle the sessions for each connection in parallel. - Beast Winterwolf
    • In your case, I recommend looking towards copas - it is just suitable for your purposes, although it works in one stream. And if you need a full multithreading - without a third-party library (there are many) can not do. - val