rayaman / multi

My multitasking library for lua
MIT License
27 stars 0 forks source link

newProxy #55

Closed rayaman closed 1 year ago

rayaman commented 1 year ago

A feature that could be used to interact with objects on another thread as if they were on the main thread.

Potential usage

local multi, thread = require("multi"):init()
local THREAD, GLOBAL = require("multi.integration.threading"):init()

local queue = multi:newSystemThreadedQueue("Test"):init()

multi:newSystemThread("Test Thread", function()
    local multi, thread = require("multi"):init()
    local q = THREAD.waitFor("Test")
    local proxy = multi:newProxy(thread:newThread(function()
        while true do
            thread.sleep(1)
            print("Doing stuff...")
        end
    end))
    q:push(proxy)
    proxy:init() -- Init after sending the proxy!
    multi:mainloop()
end)

multi:newThread(function()
    local proxy = thread.hold(function()
        return queue:pop()
    end)
    proxy:Pause() -- This would pause the co-routine based thread on the system thread. If our proxy object were a Loop Object then it would stop that. I want to create an easier interface to work with threads.
end)

multi:mainloop()

If done right, we could have an easy way to interact with objects withing a thread and transfer data.

rayaman commented 1 year ago

Should work with all objects now, does not support certain returns, threads and connection objects cannot be returned. Due to coroutines not being allowed to be transferred between threads

rayaman commented 1 year ago

Custom objects you create will work too, you cannot set variables on an object. Only call functions!