Jump to content

[SOLVED] Where and how can I start new threads


photo

Recommended Posts

Posted (edited)

Hi guys,

I'm new to Unigine and I wonder if I can create new threads (.NET or C++ threads) in the background to listen and parse data sent from other parties through TCP/UDP, so that the data receiving and parsing is not limited the Update method?

The background for this question is that I have some already-tested libraries written in C# and I'd like to reuse it, so my traditional 2D desktop application written in WPF can turn into a fancy 3D emulation application.

Thanks for any comments/reply!

Edited by luoczeng
Posted

Or say, what is the 'standard' pattern for this kind of request? Should I put my data receiving and parsing logic inside a component's Update method?

Posted

Hello!

There are no restrictions on creating threads on the engine side. You can create and use any threads. The main thing you need to remember is that the engine is not thread-safe, so it's better to move nodes or modify any parameters in the update.

Here is an article that explains in more detail what can and cannot be done in different threads. https://developer.unigine.com/en/docs/2.19.1/code/fundamentals/thread_safety/?rlang=cpp&autotranslate=en

When working with networking, the main pattern is to move the socket read and write logic to another thread, as these operations can be blocking and cause spikes in the frame rate. However, at the beginning of update, we simply retrieve the received and parsed data from the socket thread, applying them in the main thread. 

image.png

In this case, blocking operations involving the queue should be kept as short as possible to avoid blocking the main thread. That is, you should not lock the queue while reading from the socket.

You can also check out examples in demos/cpp_component_samples/networking, which include an example of an asynchronous queue for making HTTP requests (image and weather) and http responce (image and camera position).

 

Posted
4 hours ago, cash-metall said:

Hello!

There are no restrictions on creating threads on the engine side. You can create and use any threads. The main thing you need to remember is that the engine is not thread-safe, so it's better to move nodes or modify any parameters in the update.

Here is an article that explains in more detail what can and cannot be done in different threads. https://developer.unigine.com/en/docs/2.19.1/code/fundamentals/thread_safety/?rlang=cpp&autotranslate=en

When working with networking, the main pattern is to move the socket read and write logic to another thread, as these operations can be blocking and cause spikes in the frame rate. However, at the beginning of update, we simply retrieve the received and parsed data from the socket thread, applying them in the main thread. 

image.png

In this case, blocking operations involving the queue should be kept as short as possible to avoid blocking the main thread. That is, you should not lock the queue while reading from the socket.

You can also check out examples in demos/cpp_component_samples/networking, which include an example of an asynchronous queue for making HTTP requests (image and weather) and http responce (image and camera position).

 

Thank you very much! You answered all my major questions. I believe I can work the rest out.

Thanks again! You're my hero and saved my day!

  • silent changed the title to [SOLVED] Where and how can I start new threads
×
×
  • Create New...