Search Unity

WebGL: Is Threading supported?

Discussion in 'WebGL' started by MicroEyes, Feb 22, 2017.

  1. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    Hi All,
    I have a complex JSON string to parse and normally deserialization process(JSON string to C# class object) happens on MAIN thread, which was consuming around 70-100ms time of game's MAIN thread and this is going to happen 2-5times per frame, which is really bad and blocking game's MAIN thread.

    As Alternative: Then i thought, i can move deserialization process to OTHER thread (System.Threading, to save MAIN thread time) and merge result to MAIN thread.

    For testing i wrote this simple code,

    Code (csharp):
    1.  
    2. void CreateCustomThread()
    3. {
    4.      try
    5.      {
    6.           Thread t = new Thread(MyWorkerThread);
    7.            t.Start();
    8.      }catch(Exception e)
    9.      {
    10.            Debug.LogError("Error while starting thread: '" + e.Message + "'");[/INDENT]
    11.      }
    12. }
    13.  
    14. const int MAX_ITERATION = 5;
    15. void MyWorkerThread()
    16. {
    17.       int l_completedIteraction = 0;
    18.       while(l_completedIteraction < MAX_ITERATION)
    19.       {
    20.           Thread.Sleep(2000);
    21.           l_completedIteraction++;
    22.       }
    23. }
    24.  
    25.  
    but its throwing "Exception: Thread creation failed.".

    1. Is THREADs not supported on WebGL platform?
    2. Is there any other way to achieve multi-threading concept on WebGL platform?




    EDIT: [Temp solution]: I had to hardcode my class type, its fields & properties in JSON library to eliminate using C# Reflection. This increased CPU performance from 200-250ms to 4-7ms and memory 600+KB to 2-5 KB.. Also i had to put ObjectPool concept for my class types in JSON library. This works for me.
     
    Last edited: Aug 14, 2017
    Prince_of_Persia and rakkarage like this.
  2. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    from the manual: "Threads are not supported due to the lack of threading supporting in JavaScript. This applies to both Unity’s internal use of threads to speed up performance, and to the use of threads in script code and managed dlls. Essentially, anything in the System.Threading namespace is not supported."

    not really. The closest thing you can use are coroutines.
     
    Tokars, SweatyChair and rakkarage like this.
  3. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    Thanks Marco, but Co-routines again uses MAIN thread for execution, so not going to work in my case.
     
  4. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    That's correct... but threads are not supported.
     
  5. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    830
    Will threads EVER be supported? if not will there be any alternative because im having problems with freeze also when loading textures, its a BIG problem when the game freeze10 times when i load 10 textures.

    But strange it doesnt freeze when playing a video using WebGLMovieTexture, wouldnt playing a VIDEO (lots of textures) more likely to freeze? but it doesnt, why cant that method be used when loading textures?
     
  6. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    Threads are on our roadmap but since we depend on browser vendors we don't have an ETA at the moment.
     
  7. jRocket

    jRocket

    Joined:
    Jul 12, 2012
    Posts:
    598
    What about web workers? Are they not appropriate for the threading that unity uses?
     
  8. kognito1

    kognito1

    Joined:
    Apr 7, 2015
    Posts:
    331
    Can't share memory with web workers atm. What Unity needs is "Shared Array Buffer" support among browsers. It's coming, but not in the initial release of wasm (same as simd support).
     
  9. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    830
    But why does it freeze when loading ONE texture, but has ZERO freeze when streaming a video? Wouldnt a video have more demands than a 128x128 png pixel image? loading that small image using WWW causes a freeze, but streaming a video has no freeze.
     
  10. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    the WebGLMovieTexture sample streams movies. Textures are loaded synchronously by the main thread, that's why it freezes.
     
  11. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    830
    The freeze is really horrible! i wonder if there is a way to stream the texture?
     
  12. Blenderik

    Blenderik

    Joined:
    May 14, 2013
    Posts:
    109
    I use coroutines for that, no freezing.
    dummy:
    Code (CSharp):
    1. Texture 2D myTexture;
    2.  
    3. void tryAssignTexture()
    4. {
    5.   if (myTexture == null)
    6.     StartCoroutine(downloadTexture);
    7.   else
    8.     assignTexture();
    9. }
    10.  
    11. assignTexture()
    12. {
    13.   myMaterial.SetTexture("_MaintTex", texture);
    14. }
    15.  
    16. IEnumerator downloadTexture()
    17. {
    18.     //... myTexture = download...
    19.     assignTexture();
    20. }
    21.  
    In case the request can be sent several times in a short interval (or just as good practice) use a global bool downloadPending, to bridge the gap between texture download request and download complete.
     
  13. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    830
    Your code looks like it will freeze also, i dont see it doing anything different from what im doing.

    Here is how you can tell if it will freeze. Make a new scene, create a simple cube, attach a script to that cube that spins the cube constantly in the update function.

    While the cube is spinning, put your coroutine in there that downloads the texture and assigns it, see if the cube ever pauses spinning for a little, if it pauses then your code freezes just like mine.

    make sure your code does it over and over, keep downloading and assigning the texture, you will see that the cube keeps stopping when you assign the texture to the cube.
     
  14. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    830
    Can some SMART engineer at Unity PLEASE create a coroutine/async version of LoadImage, or LoadRawImage that doesnt freeze the main thread? This is so badly needed because my game and so many other people games are messed up because of this and there is no way around it because those are the only functions we can use use to assign textures, and its on the main thread that freeze the game.

    My game is especially horrible because im loading many LARGE images from the internet and applying them to various objects in the scene all the time during the game, and its really horrible to watch it glitching like mad.
     
  15. hirokryptor

    hirokryptor

    Joined:
    Oct 19, 2019
    Posts:
    1
    Did you have any solution for it?
    Load tons users provide images url make my game is very lag.
     
unityunity