Threads in Coppeliasim

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

Threads in Coppeliasim

Post by ArthurS »

Hello, the site administrator has recently helped me, for this many thanks to him, to solve the problem of multi-threaded interaction. I would like to know exactly how threads work in this program. Namely, the internal component, what's the difference with streams from c ++ and a little about "coroutine".

I speak right away. I read the documentation about all this on the official website. That is written in multiples and I did not get the point. Explain "in an accessible way" please.

fferri
Posts: 1217
Joined: 09 Sep 2013, 19:28

Re: Threads in Coppeliasim

Post by fferri »

Given your previous posts, I'm guessing you want to know the difference between threaded child scripts and non-threaded child scripts.

Non-threaded child scripts execute synchronously with simulation.
Once per timestep, the main script is executed, which takes care of executing all child scripts (i.e. calling the various sysCall_sensing, sysCall_actuation, etc... callback functions). If any code inside non-threaded child scripts takes long to execute, it will block the whole simulation, as the child scripts are run sequentially and synchronously.

Threaded child scripts execute asynchronously, meaning they will not execute callback functions on specific events, but rather execute a single function independently from the simulation flow. Threaded scripts can execute long computations without blocking the simulation loop. Nowadays, threaded scripts have been re-implemented as Lua coroutines (pieces of computation that can be paused and resumed), so your threaded function (which used to be sysCall_threadMain) is coroutineMain().
Whatever you run in coroutineMain() executes independently of the simulation and can also execute across multiple timesteps. This is achieved by periodically interrupting long-running coroutines, which are then resumed at the next timestep with a call to coroutine.resume() which you can see in the default threaded script code.

If, in a threaded script, you don't want that a piece of code is interrupted in the middle and resumed at next timestep, you can temporarily disable thread switching:

Code: Select all

local sl=sim.setThreadAutomaticSwitch(false)
-- execute here code that will not be interrupted
-- by coppeliaSim scheduling policy
sim.setThreadAutomaticSwitch(sl)
See also:

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

Re: Threads in Coppeliasim

Post by ArthurS »

Thank you for the clarification. You can even more detail about the device of threads from the inside out. And also look at my question on August 28 about exe extension 36021 post id. Maybe you can do something to help.

Post Reply