When a function is executed a new thread object is created?

4

According to the manual of version 5.3, a value of type thread represents independent threads of execution:

  

The type thread represents independent threads of execution and it is used to implement coroutines (see §2.6). Moon threads are not related to operating-system threads.

Does this mean that every time the body / scope of a function runs a new thread is created? Or does it happen with every kind of scope? For example:

-- Cria um thread (principal)

(function()
    -- Cria um thread
    for i = 1, 2 do
        -- Cria outro thread
    end
end)();

Or with functions only:

-- Cria um thread (principal)

(function()
    -- Cria um thread
    for i = 1, 2 do
    end
end)();
    
asked by anonymous 07.01.2017 / 14:41

1 answer

3

Question : Does this mean that every time the body / scope of a function runs a new thread is created? >

Question : Is this true for all types of scopes?

Explanation : A "thread" (when speaking the Lua language) is created only when you create a coroutine coroutine.create and coroutine.wrap .

Reference: link

Example usage: link

By the way, I find this style of coding:

(function()
   -- Cria um thread
   for i = 1, 2 do
       -- Cria outro thread
   end
end)();

which is fairly common in JavaScript is not normally used with Lua.

    
07.01.2017 / 19:54