Option to execute custom time consuming tasks without blocking main thread.
current functions list, its syntax and purpose:
Function availiable only in thread:
Maybe:
bool isMainThread() -- return true if called in main thread, otherwise false.
setThreadOutputHandler(thread, callback) -- to set where are outputs from print, outputChatBox ect. will redirected,
setThreadErrorHandler(thread, callback) -- to set where to output errors
setThreadCustomHandler(thread, callback) -- used for communication from thread, to main thread ( i don't have idea how to name it )
threadLoadstring(thread, code) -- to load extra code into thread ( if while creating you explicit allow it in options )
getThreadsLimits() -- Clientside, returns information about user settings, how many threads can be created ect.
getThreadtimings(thread) -- https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadtimes
startThread(thread) -- Starts the thread.
pauseThread(thread) -- Pause the thread. -just calling "sleep" function all time until "start" get called
Maybe in future, option to create thread-safe variable, name based on std::atomic
atomic createAtomic( ... )
values getAtomicValue( atomic )
bool setAtomicValue( ... )
Other:
- "threadCall" wil have option to get return of that function, but due problems with argument parser right now you can't do that.
- while creating new thread, you should have options to a bit more customize it. I'm consider that useful functions would be to set what will loaded by default, what functions, if possible, option to change "speed" of that thread by adding sleep(1) every n instructions,
- Clientside threads are a bit dangerous eg. malicious code that will run infinity loop to consume all cpu time. Client should have option to somehow limit, slowdown custom threads
- Only thread-safe functions should be availiable inside
- Errors, how to handle them? i wrote proposal function above
- Should we limit threads serverside, add mtaserver.conf option to limit how many of them can running at the same time? or you can create as many threads as you want?
- ACL, by default all functions should be disallowed. When thread get created, it should inherit permissions from resource that created this thread
Current example:
code = [[
function mineBitcoins()
for i=1,500000000 do
end
print("1 bitcoined nined")
end
function add(a,b)
print("add",a,b)
return a+b
end
print("hello, i'm thread")
return 1,2,3
]]
addEventHandler("onResourceStart", resourceRoot, function()
thread = createThread(code)
local state = ""
i = 0
function update()
i = i + 1
local newState = getThreadState(thread)
if(state ~= newState)then
state = newState
print("state =", state)
end
if(state == "loaded")then
print("result",getThreadResult(thread))
threadCall(thread, "add", {i, 10})
threadCall(thread, "mineBitcoins")
end
end
update()
setTimer(update,0,0)
end)
image
Uh oh!
There was an error while loading. Please reload this page.
Option to execute custom time consuming tasks without blocking main thread.
current functions list, its syntax and purpose:
createThread(string code)-- Creates a new thread and start executing code which in that threadgetThreadState(thread)-- Return current state of threadthreadCall(thread, functionName, table of arguments)-- calling function inside thread.killThread(thread, terminate)-- sends signal to kill thread, wait 1 second ( if second parametr is false ) and then terminates a thread forcefully, ending its execution.getThreadFailureReason(thread)-- In case of error thrown insidethreadGetResult(thread)-- Returns value thread returned at the end ( if something have been returned and code reached end )getThreads()-- return all threadssetThreadShutdownHandler(thread, callback)-- when "exit" get called inside threadFunction availiable only in thread:
sleep(ms)--exit(...)-- stopping thread, send data to shutdown handlersendMessage(...)-- sends message that you can catch usingsetThreadCustomHandlerin main threadMaybe:
bool isMainThread()-- return true if called in main thread, otherwise false.setThreadOutputHandler(thread, callback)-- to set where are outputs from print, outputChatBox ect. will redirected,setThreadErrorHandler(thread, callback)-- to set where to output errorssetThreadCustomHandler(thread, callback)-- used for communication from thread, to main thread ( i don't have idea how to name it )threadLoadstring(thread, code)-- to load extra code into thread ( if while creating you explicit allow it in options )getThreadsLimits()-- Clientside, returns information about user settings, how many threads can be created ect.getThreadtimings(thread)-- https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadtimesstartThread(thread)-- Starts the thread.pauseThread(thread)-- Pause the thread. -just calling "sleep" function all time until "start" get calledMaybe in future, option to create thread-safe variable, name based on std::atomic
atomic createAtomic( ... )values getAtomicValue( atomic )bool setAtomicValue( ... )Other:
Current example:
image