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

Asked

Viewed 118 times

4

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

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 is executed a new thread is created? Or does this happen with all kinds 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)();
  • I don’t understand where you got your interpretation from. "The Type thread represents independent thread execution and is used to implement coroutines. What do you mean by block? A if, one loop? If it is, it’s a matter of language scope.

  • @Gerep When I refer to the block I refer to a list of statements (a statement is an statement that is not an expression)

1 answer

3


Question: this means that every time the body/scope of a function is executed a new thread is created ?
Answer: nay.

Question: or this happens with all kinds of scope ?
Answer: nay.

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

Reference: http://www.lua.org/manual/5.1/manual.html#2.11

Example of use: https://www.tutorialspoint.com/lua/lua_coroutines.htm

By the way, I think this style of coding:

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

which is quite common in Javascript is usually not used with Lua.

  • Regarding the coding style, I just wanted to try to exemplify my doubt to the functions. I might not have used a function, but rather one of, or even needed the, but my doubt is more tied to functions as well. I was beginning to think that the thread would be a reference to statements that are currently running in a list of statements / or function.

  • to tell the truth I don’t even know if this type of coding is accepted, I haven’t tested but I suspect that

  • It is valid, but expressed functions cannot specify name or name of members, and local functions can only specify names: local function a() end, function a:x() end function a.y() end function b() end. Already the semicolon, as far as I know has always been supported, but in some version of the Moon it seems to be misinterpreted. The luaparse I know doesn’t let the semicolon be a statement when it doesn’t delimit statements.

  • you’re right, I tested it here and it worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.