Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Multithreading #1964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
CrosRoad95 wants to merge 11 commits into multitheftauto:master from CrosRoad95:createThread
Closed

Multithreading #1964

CrosRoad95 wants to merge 11 commits into multitheftauto:master from CrosRoad95:createThread

Conversation

@CrosRoad95
Copy link

@CrosRoad95 CrosRoad95 commented Dec 30, 2020
edited
Loading

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 thread
  • getThreadState(thread) -- Return current state of thread
  • threadCall(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 inside
  • threadGetResult(thread) -- Returns value thread returned at the end ( if something have been returned and code reached end )
  • getThreads() -- return all threads
  • setThreadShutdownHandler(thread, callback) -- when "exit" get called inside thread

Function availiable only in thread:

  • sleep(ms) --
  • exit(...) -- stopping thread, send data to shutdown handler
  • sendMessage(...) -- sends message that you can catch using setThreadCustomHandler in main thread

Maybe:

  1. bool isMainThread() -- return true if called in main thread, otherwise false.
  2. setThreadOutputHandler(thread, callback) -- to set where are outputs from print, outputChatBox ect. will redirected,
  3. setThreadErrorHandler(thread, callback) -- to set where to output errors
  4. setThreadCustomHandler(thread, callback) -- used for communication from thread, to main thread ( i don't have idea how to name it )
  5. threadLoadstring(thread, code) -- to load extra code into thread ( if while creating you explicit allow it in options )
  6. getThreadsLimits() -- Clientside, returns information about user settings, how many threads can be created ect.
  7. getThreadtimings(thread) -- https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadtimes
  8. startThread(thread) -- Starts the thread.
  9. 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

  1. atomic createAtomic( ... )
  2. values getAtomicValue( atomic )
  3. bool setAtomicValue( ... )

Other:

  1. "threadCall" wil have option to get return of that function, but due problems with argument parser right now you can't do that.
  2. 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,
  3. 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
  4. Only thread-safe functions should be availiable inside
  5. Errors, how to handle them? i wrote proposal function above
  6. 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?
  7. 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

teebreak, VadimMuhtarov, gta191977649, onikyannn, and Wannacry-ops reacted with thumbs up emoji MegadreamsBE, Unde-R, Gallardo994, itstheodor, xman40100, gta191977649, and Wannacry-ops reacted with hooray emoji Dante383, turret001, itstheodor, xman40100, gta191977649, and Wannacry-ops reacted with heart emoji xLive, Einheit-101, MegadreamsBE, Unde-R, Gallardo994, turret001, AlexTMjugador, pkfln, gta191977649, onikyannn, and Wannacry-ops reacted with rocket emoji
@patrikjuvonen patrikjuvonen linked an issue Dec 30, 2020 that may be closed by this pull request
@patrikjuvonen patrikjuvonen added the enhancement New feature or request label Dec 30, 2020
Copy link

This is cool.

MegadreamsBE, Gallardo994, and Wannacry-ops reacted with laugh emoji

Copy link

prnxdev commented Jan 5, 2021

OOP support pls

XeNikk and Wannacry-ops reacted with laugh emoji

Copy link
Contributor

Is it possible to change createThread(string code) to createThread(function) ?

Copy link
Author

Is it possible to change createThread(string code) to createThread(function) ?

yes

Deihim007 reacted with hooray emoji

Copy link
Contributor

Is it available on serverside or clientside?

Copy link
Member

Lpsd commented Mar 29, 2021

Both, refer to the code

gta191977649 reacted with thumbs up emoji

Copy link
Contributor

github-actions bot commented Jan 7, 2022

This draft pull request is stale because it has been open for at least 90 days with no activity. Please continue on your draft pull request or it will be closed in 30 days automatically.

@github-actions github-actions bot added the stale Inactive for over 90 days, to be closed label Jan 7, 2022
Copy link
Contributor

github-actions bot commented Feb 7, 2022

This draft pull request was closed because it has been marked stale for 30 days with no activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

enhancement New feature or request stale Inactive for over 90 days, to be closed

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Add multi-threading support to Lua

AltStyle によって変換されたページ (->オリジナル) /