multithreading of scripts of several objects . multithreaded data processing

Typically: "How do I... ", "How can I... " questions
Post Reply
ArthurS
Posts: 28
Joined: 25 Jul 2021, 20:00

multithreading of scripts of several objects . multithreaded data processing

Post by ArthurS »

Hello. I have some drones from rotots\mobile\Quadcopter.ttm. They are loaded as models.


They have the same code of scripts that describe the movement.
I have n tasks to which drones are flying.
Drone 1 takes task 1, drone 2 takes task 2, drone k takes task k , and so on while drones or tasks are free.
If, for example, drone 1 has reached and completed its task, it takes the next free task from the list.
I organized this process like this:

Code: Select all

function writePOINTArray()
      sim.setStringSignal('POINTArray',sim.packTable(POINTArray))
end
function readPOINTArray()
    local data=sim.getStringSignal('POINTArray')
    if data then
        data=sim.unpackTable(data)
    else
        data={}
    end
    return data
end

During the flight, each drone "monitors" the same data array (PointArray)-an array of references to points, which lies in the "StringSignal". And if the drone decides to fly to the next point, it changes the value of the i element of the array to -1. Thus , the next copter simply does not see this point and takes the next value != -1.

But before I write -1 to the desired position in the array, I need to get the latest-up-to-date copy of this array. I read it using readPOINTArray, set -1 and write it again using writePOINTArray.
Naturally, to avoid problems with multithreading, I use the block

Code: Select all


sim.setThreadAutomaticSwitch(false)
....
sim.setThreadAutomaticSwitch(true)

Inside which I call readPOINTArray and writePOINTArray.

My question is: did I organize this process correctly?
And I still haven't figured out how the

Code: Select all

sim.setThreadAutomaticSwitch(false)
....
sim.setThreadAutomaticSwitch(true)
block works

Does it suspend the multithreaded execution of only this script in which it is written , or the entire program and scripts at that moment while we are in it ??


I really hope for your answer!!. Thank you for your patience and help!

coppelia
Site Admin
Posts: 10649
Joined: 14 Dec 2012, 00:25

Re: multithreading of scripts of several objects . multithreaded data processing

Post by coppelia »

Hello,

yes, this is how you should do it. Actually, it would be even better with:

Code: Select all

    local lb=sim.setThreadAutomaticSwitch(false)
    ... (e.g. read/write common data)
    sim.setThreadAutomaticSwitch(lb)
since above allows to have cascading locking/unlocking.

When you forbid a threaded script to switch (or yield), then nothing else will be running except that script. i.e. everything else will be blocking.

Cheers

ArthurS
Posts: 28
Joined: 25 Jul 2021, 20:00

Re: multithreading of scripts of several objects . multithreaded data processing

Post by ArthurS »

OK. thanks for the clarification.

Post Reply