N
N
napo1eon2020-11-22 02:14:49
Lua
napo1eon, 2020-11-22 02:14:49

How to call main thread from created lua lanes thread?

hello .. who worked with the Lua Lanes library? I didn’t think that it would be so difficult in lua with threads .. I want to start the timer in a separate thread so that it calls the function from the main thread every 2 seconds .. while the main thread is not looped .. the problem is that I can’t call the function main2 in the main thread ... for this reason it is not clear how to call functions from your program .. tell me please ..

local linda = lanes.linda()

local main2 = function(str)
    --core.ErrorMassage(str)
    print("!!!!!!!!!")
end

_G = {["m"] = main2}

    f = function(str) core.ErrorMassage("sleep "..str) end

    lanes.gen("*",{ globals =  _G }, function(  ) 
--        lanes.require ("core")   
        print("start!!!!")
        while true do
            local clock = os.clock
            local t0 = clock()
            while clock() - t0 <= 2 do end
            m()       
        end
        print("and")
    end ) (  )

    print("ands")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Tomatov, 2020-12-13
@16tomatotonns

Despite its apparent simplicity, lua lanes, at the moment a thread spawns, creates a copy of the executable function of this thread (with a copy of all closures), passes it to the new lua_State and sends it for execution.
In this model, all communication should take place in a messaging system: the child thread sends messages to the main thread, the main thread reads and responds. There is no direct feedback through functions, such things should be arranged at the level of the language itself and not third-party libraries.
A typical scheme for working with lua lanes is a thread manager that assigns tasks to them and reads the responses.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question