Multi-threading in C++ plugin

Typically: "How do I... ", "How can I... " questions
Post Reply
galois10
Posts: 3
Joined: 31 Oct 2014, 11:06

Multi-threading in C++ plugin

Post by galois10 »

Dear all,
I would like to know if it is possible to launch multiple threads (using ubuntu's pthreads) within a C++ plugin. Is it supported (and safe) in VREP?
I have found information about multi-threading in child scripts, but not for plugins.

Thank you in advance!

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

Re: Multi-threading in C++ plugin

Post by fferri »

The V-REP API is not thread safe. Ensure that you call API functions from the SIM thread. Please refer here for which messages are sent from which thread. The v_repStart function is called from the UI thread. The v_repMessage function can be called from the UI thread or from the SIM thread, depending on which message.
  • One possible way to work with multiple threads is to have some sort of synchronized queue, and handle it in v_repMessage (in a message sent from the SIM thread, such as sim_message_eventcallback_instancepass). You can create how many threads you want, but use the synchronized queue to communicate with the application, so that every API function is called from the SIM thread.
  • Another way, if you use Qt, is to use QObject signal/slot connections. You can create a QObject in the SIM thread, and another QObject in your thread, and make them communicate with signal slot connections. Each QObject will have its own message loop which will run in the thread the QObject was constructed in (see thread affinity). See Threads and QObjects, and in particular Signals and Slots Across Threads.

Post Reply