What is multithreading & does MonkeyX support it?

Monkey Forums/Monkey Beginners/What is multithreading & does MonkeyX support it?

StoneFaceEXE(Posted 2014) [#1]
I just can't wrap my head around it, a simple example of multithread and not multithread app would be cool


Paul - Taiphoz(Posted 2014) [#2]
not sure there is a simple example, there are some modules that add it although i'v not used it myself, diddy for example I am sure comes with a threading module, I am sure some others probably will as well.

Threading in essence is the ability to have your game or app run more than one thing at the same time, just imagine how your code is executed one line after another, you call a function your code jumps into the function does the task then exist , it wont do anything else until it has complete the function its in, with threading you can fire off multipule threads each of which are able to execute their own code all at the same time while your main game chugs along doing other things.

Threading can be used for a number of things, the last tool I wrote with threading in was a er, scanner the tool would look at a range of addresses do some funky magaic and return some results, without threading my tool checked only one at a time, it would connect wait until it got a response , store the result and then move onto the next address, but once I implemented threading I was then able to queue up loads of addresses and fire them off in threads which allowed the main tool to keep looking through new addresses while the other threads did their things.

Threading also adds a whole other level of complexity to your code, if you start a thread which does something or returns something that's important to the execution of something else in your code then you need checks and balances to make sure that the information is there before you continue, its really easy to lose a thread and crash your code.

sorry if thats all a little vague its been a while since iv done any threading at all. I am sure one else will pop along shortly with a much better explanation.


StoneFaceEXE(Posted 2014) [#3]
No, your explanation is great like, literaly. I'd love to know more if Threading is affected by the ammount of CPU cores, in case it is affected that is. But knowing monkey supports threading is pretty much enough, I should go and inspect that diddy module then. Thanks a lot!)

In case anybody have something to add - that would be cool too)


AdamRedwoods(Posted 2014) [#4]
Monkey doesn't REALLY support multi-threading. the garbage collector will not work properly in different threads.

I haven't tried out the Diddy threading module, but the Monkey threading module doesn't cover all platforms and is very restrictive.


Samah(Posted 2014) [#5]
@AdamRedwoods: Monkey doesn't REALLY support multi-threading. the garbage collector will not work properly in different threads.

Diddy's multithreading module (actually it's a side-module) does indeed use native threads, which means it works on almost all targets that support threading. As you mention, Monkey's own garbage collection is not guaranteed to be 100% accurate when interacting with user threads, however the native GC for the target platform (if it has one) will generally work fine.

In the future I may investigate ways of handling this, but for now the best option is to only use user threads for processing existing data and not for creating many objects.