Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Threads — Gideros Forum

Threads

rrraptorrrraptor Member
edited April 2021 in General questions
I was wondering how to multithread things :smile:
For example, we have 8 grids where each grid have an array of 32*32 elemnts (in total 8192 iterations). Lets say that this grids do not share any information, so they selfcontained objects. I need to update each grid every frame and loop over every (or maybe not) element and do something with it.
I came up with this:
https://github.com/MultiPain/Gideros_examples/tree/master/Multithreading

So, the question: is it correct? Or there is a nicer way?)

For me it seems correct.
16 improvised grids of [64x64] size: 65536 iterations per frame
No threads: ~0.025 seconds full cycle (40 fps)
Threads: ~0.0045 seconds full cycle (60 fps)

32 "grid" [128x128]: 524288 iterations
No threads: ~0.18 seconds full cycle (5 fps)
Threads: ~0.016 seconds full cycle (60 fps)

Specs: i7-2600k 3.4GHz (8 logical cores, 8 threads)

Likes: MoKaLux

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • rrraptorrrraptor Member
    edited April 2021
    However, there is a big problem. In player mode, thread worker works ok, but only at the very first start. The next one causes the player to freeze and crash.
  • rrraptorrrraptor Member
    edited April 2021
    Also, threads working only with COPY of your data that can be serialised, so there is no way to use them with any type of graphics. @hgy29 is that correct? And inside a thread you do not have access to any plugin except for these: socket.core, lfs, json, json.safe, mime.core.
    So...thread can only do some basic operations.
  • MoKaLuxMoKaLux Member
    edited April 2021
    I tested your code but it crashes the player even on the first launch.
    if (USE_THREADS) then 
    	local CORES = Thread.getNumLogicalCores()
    	print(CORES)
    	-- use all logical cores
    	worker = Worker.new(CORES)
    end
    But when you don't use all available CORES that doesn't crash the player anymore :)
    	worker = Worker.new(CORES/2) -- CORES-2 perhaps works too?
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • MoKaLux said:

    I tested your code but it crashes the player even on the first launch.

    if (USE_THREADS) then 
    	local CORES = Thread.getNumLogicalCores()
    	print(CORES)
    	-- use all logical cores
    	worker = Worker.new(CORES)
    end
    But when you don't use all available CORES that doesn't crash the player anymore :)
    	worker = Worker.new(CORES/2) -- CORES-2 perhaps works too?
    Yeah, its not stable. Maybe depends on processor...?
    i7-2600k 8 cores - OK
    i7-8600 12 cores - OK
  • hgy29hgy29 Maintainer
    rrraptor said:

    Also, threads working only with COPY of your data that can be serialised, so there is no way to use them with any type of graphics. @hgy29 is that correct? And inside a thread you do not have access to any plugin except for these: socket.core, lfs, json, json.safe, mime.core.
    So...thread can only do some basic operations.

    Yes, this is correct, mainly due to the fact that Gideros graphic API isn't thread safe.
Sign In or Register to comment.