luoczeng Posted yesterday at 01:41 AM Posted yesterday at 01:41 AM (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 yesterday at 01:43 AM by luoczeng
luoczeng Posted yesterday at 05:42 AM Author Posted yesterday at 05:42 AM 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?
cash-metall Posted 22 hours ago Posted 22 hours ago 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. 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).
luoczeng Posted 17 hours ago Author Posted 17 hours ago 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. 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!
Recommended Posts