Search Unity

[RELEASED/FREE] Thread Ninja - Run co-routines on background thread

Discussion in 'Assets and Asset Store' started by ciela_spike, Mar 7, 2014.

  1. ciela_spike

    ciela_spike

    Joined:
    Jan 1, 2014
    Posts:
    3
  2. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Thanks for the release.
     
  3. karthikrock

    karthikrock

    Joined:
    Sep 6, 2014
    Posts:
    3
    Hi, I am trying to load a RevMob banner Ad from the background thread but it doesnt work and crashes the android game.. This is how i do it:


    private static readonly Dictionary<String, String> REVMOB_APP_IDS = new Dictionary<String, String>() {
    { "Android", "MYID"},
    { "IOS", "copy your iOS RevMob Media ID here" }
    };
    static RevMob revmob;
    static RevMobBanner banner;


    public void Start ()
    {
    revmob = RevMob.Start(REVMOB_APP_IDS, "Main Camera");

    }



    public void PowerUpsButtonClick ()
    {

    // other code
    StartCoroutine (StartExamples ());

    }




    IEnumerator StartExamples()
    {

    Task task;
    this.StartCoroutineAsync(Blocking(), out task);
    yield return StartCoroutine(task.Wait());


    }


    IEnumerator Blocking()
    {
    LogAsync("Revmob loading banner");
    if( revmob != null) {
    banner = revmob.CreateBanner(RevMob.Position.BOTTOM, 0, 0, Screen.width, 170);
    }

    LogAsync("Jump to main thread.");
    yield return Ninja.JumpToUnity;
    banner.Show ();

    }


    The game crashes out and I get the error :
    10-31 14:51:12.496: E/dalvikvm(10886): JNI ERROR (app bug): accessed stale local reference 0x1d200001 (index 0 in a table of size 0)
    10-31 14:51:12.496: E/dalvikvm(10886): VM aborting
    10-31 14:51:12.716: E/MP-Decision(2179): num online cores: 2 reqd : 1 available : 4 rq_depth:0.000000 hotplug_avg_load_dw: 26
    10-31 14:51:12.716: E/MP-Decision(2179): DOWN cpu:1 core_idx:1 Ns:1.100000 Ts:190 rq:0.000000 seq:197.000000


    10-31 14:51:13.377: A/libc(10886): Fatal signal 6 (SIGABRT) at 0x00002a86 (code=-6), thread 12633 (GC_start_routin)



    Can you pls help me out on this one ?
     
  4. ciela_spike

    ciela_spike

    Joined:
    Jan 1, 2014
    Posts:
    3
    Hi, I think it's just not allowed to do this on another thread, you must change the UI on the main thread. Background thread is usually used for performing heavy computation.
     
  5. karthikrock

    karthikrock

    Joined:
    Sep 6, 2014
    Posts:
    3
    Hi Thanks for ur reply..

    Can you please give me an example as to what kinds of things can be performed by this library?
     
  6. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,335
    @Karthikrick, let's say you want to calculate lot of noise function from
    http://forum.unity3d.com/threads/68764-LibNoise-Ported-to-Unity
    You do something like this
    Code (CSharp):
    1.         for (int i=0; i<threadCount; i++) {
    2.             int min = i * iterations / threadCount;
    3.             int max = (1 + i) * iterations / threadCount;
    4.             this.StartCoroutineAsync (Compute (min, max));
    5.         }
    To split Compute amongst threadCount threads.

    Where Compute is this call to the beast that is noiselib
    Code (CSharp):
    1.     IEnumerator Compute (int min, int max)
    2.     {
    3.         Debug.Log (min + " - " + max);
    4.         for (int i=min; i<max; i++) {
    5.             perlins [i] = (float)perlin.GetValue (i, 0, 0);
    6.         }
    7.         computed++;
    8.         yield return Ninja.JumpToUnity;
    9. // do some Unity stuff like generate cubes based on noise value
    10.     }    
     
  7. kuchaku

    kuchaku

    Joined:
    Oct 14, 2014
    Posts:
    37
    Thank you for making this asset, Ciela Spike.This seems very useful and so few have tried it or commented on it.

    When I first learned of 'co routine' I originally thought they were something like this, 'co processing' a routine. Now they actually can and do, improving performance significantly over a regular co routine.

    Being able to jump in and out of the main thread is useful. If Unity's API isn't thread safe, something like this seems like a nice compromise. I'm actually a little curious as to why Unity itself wouldn't pursue a similar feature. C# isn't known for its light speed, multi cores can be very useful, simplifying them and making it as clear as possible when in a backing thread or the main thread seems very intuitive to me and goes well with the idea of more rapid development.
     
  8. kookyoo

    kookyoo

    Joined:
    Apr 19, 2010
    Posts:
    53
    Hi,
    First of all thanks a lot for sharing such great tool with us, it's pretty powerfull :)
    I'm trying some load tests using your system, and found that over 3 very hungry tasks at the same time cause a huge performance drop in Unity. I suppose it's related to one of the threads being executed on the same core as Unity's main thread.

    As we know that Unity uses cores 0 & 1 for most of it's operations, don't you think it could be usefull to be able to set affinity to the thread pool to avoid those cores ? I made some researchs but didn't found how to plug it easily in your system. So I really would like to have your opinion on this idea.

    Again, Thanks a lot !
     
    TooManySugar likes this.
  9. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    Hi! I'm new to unity and programming in general (lone artist/designer), also the idea of threading is completely new to me. So, I'm having a bit of trouble understanding the concept behind the usage of the code (I've never used threading before and don't use coroutines too often)... Could anyone give me a basic explanation about the reason behind the workflow/the logic behind the code? About the background and foreground threads and the kinds of things I'm supposed to do in one but not the other?

    I'm sorry if I'm being too silly, but I really want to use this whenever I can but I'm having a hard time understanding when to use it and how to use it properly. But the idea of better performance is just too juicy to pass up. Also, would it be possible to use this with MEC (More effective coroutines)? it sounds like it would be a golden combination (MEC coroutines are twice as fast as unity's and don't create any GC).

    Anyway, any advice would be greatly appreciated.
     
  10. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,335
    @Alverik If bits of your game code does heavy calculation and you don't want your game framerate to drop or your game to freeze up while that's happening, you can run that bit of code on a separate thread. Then when you need to access the Unity API (for example to change the position of a gameobject, or refresh an array of particles), you jump to the main thread using
    Code (CSharp):
    1. yield return JumpToUnity();
    MEC is for spawning 1000s of coroutines, usually an indication that you're doing something wrong. I explain: if you have 1000s of characters doing stuff and display isn't the bottleneck, it'll be so hard to debug that you'll spend most of your time figuring out who does what, coroutines are awful when it comes to debugging. The better alternative is to centralize all your character actions, store states in big arrays and process all that in one loop, handling timing with counters. Added benefit is it's much much more performant.
     
    Alverik likes this.
  11. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    Thanks, great info by the way, still haven't had to control such a huge amount of characters but that's handy to know. Still, I have a bit more questions. My main confusion is how this is going to be processed. The order. So, If, as I've understood, you start in the background thread, which is supposed to be used for heavy calculations, then when I use yield return JumpToUnity() will the next code be done immediately or will the system wait for the previous code to be done to run it? This confuses me because isn't the whole idea from threading to do two different things at the same time in separate threads? or am I misunderstanding how threading works or is used in unity?...

    Again I'm sorry, I've only started to learn C# like 3-4 months ago... So I'm still very green... (I did read a book about C once, but that was like 10 years ago...)
     
    Last edited: Aug 11, 2016
  12. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Best avoid subjects that are problematic for really experienced programmers then. Stick to main thread. Avoid co-routines. Instead do it normally and directly.

    When the game is finished, run the profiler to understand where to optimise. This will be surprising - it will generally be slow where you least expect it, and will be something that co-routines and threading wouldn't have helped with anyway. Those things seldom help unless people know precisely what their program's pressure points are.
     
    laurentlavigne and Alverik like this.
  13. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,335
    -Like Hippo says-

    Ill answer anyway to confuse you so you run away from this tricky topic: Ciela uses dotNET coroutine so all the stuff that you use in NinjaThread behaves the same: yield return JumptToUnity() is a yield so it waits until the previous operation is done plus a frame then executes the rest.
    Check out Ninja's doc and if your'e confused about timing and game loop, build some tests with Debug.Log("doing dis " + Time.time); because it's very hands-on stuff and all your questions can be answered by a few hours of you testing simple examples.

    For example do some permutations of:
    Code (CSharp):
    1. public UI.Text text;
    2. void Start()
    3. {
    4.     StartCoroutineAsync(ThreadedThing());
    5. }
    6.  
    7. IEnumerator ThreadedThing(){
    8.     Debug.Log("starting "+Time.time);
    9.     text.text ="I start";
    10.     yield return JumpBack();
    11.     Debug.Log("in a separate thread "+Time.time);
    12.     text.text ="I crash!"; // API access, should be commented out
    13.     var thing = 0;
    14.     for(int i=0; i<100000000; i++){
    15.         thing += Mathf.sin(i);
    16.     }
    17.     yield return JumpToUnity();
    18.     Debug.Log("finished without freezing the game "+Time.time);
    19.     text.text = "finished without freezing the game "+Time.time.ToString();
    20. }
    Play around commenting and re-ording code inside ThreadedThing and see what breaks. text.text will, 99.99% of unity API calls will, Debug.Log or Mathf. won't crap out - very hands on.

    Stepping into async execution without solid foundation in c# is playing with fire, and at least familiarize yourself with the unity unique gameloop: http://docs.unity3d.com/Manual/ExecutionOrder.html
     
    hippocoder and Alverik like this.
  14. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    Thanks, I'll take both your tips. I'll definitely check out the docs and try testing giving the code a spin. I'll also try to limit it's use to processes I think are quite heavy. Or as Hippocoder said, I'll wait until I see what's making my game slow and see if the technique is applicable to the issue. Anyways, one way or another I want to learn how to use it right, so thank you both.
     
    hippocoder likes this.
  15. Trinary

    Trinary

    Joined:
    Jul 26, 2013
    Posts:
    395
    What you said there is not really true.

    MEC is an improved implementation of Unity's coroutines. MEC has a range of extra features, runs faster, and uses less memory. In almost every case MEC coroutines will perform a lot better than the action arrays you are talking about here, laurentlavigne, since MEC is basically the same structure under the hood except it is also self-optimizing.

    You would want something like MEC if you wanted to be able to run coroutines from non-Monobehavior classes, were using them heavily, needed more features and speed, or wanted to stop the garbage collector from causing frame rate jitters. You would use something like Thread Ninja if your app is running really heavy computations and you want to be able to do that without causing framerate jitters.

    The two packages address different potential problems, so you have to ask yourself which of those problems you want to address, and keep in mind that if your answer is "none of those" then just use Unity's coroutines.
     
    Alverik likes this.
  16. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    Yeah, I was thinking of using both anyways ;) (whenever they are necessary or applicable).
     
    Last edited: Aug 11, 2016
  17. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,335
    @Trinary the "self-optimizing" comment had me take a second look at MEC, is it RemoveUnused that you're talking about?
     
  18. Trinary

    Trinary

    Joined:
    Jul 26, 2013
    Posts:
    395
    @laurentlavigne A quick overview of the structure is that each coroutine is a process that gets put into a (coroutine) process list. The processes run one after another for as long as they are alive. Any process that finishes gets culled from the list automatically. MEC saves processing time by leaving holes in its process list and RemoveUnused comes by occasionally and removes those holes.

    You were saying you could take all your possible actions and store them in a big array. An array like that would have to check for every possible action either every tick or every frame. You could upgrade such a structure with a system to register events if you liked, and then only process the events that were in use, but that would be basically reproducing a process pump. If you also took advantage of the yield return architecture then what you would have there would be a coroutine system. That's what MEC is: A highly optimized coroutine system.

    What I was saying is that a process pump is a self-optimizing structure when compared against an array of processes, and all coroutine systems are process pumps.
     
    idbrii and Alverik like this.
  19. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,335
    @Trinary I only use yield return in the context of unity coroutine, can you explain how those work?
    http://data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxMSEhUSEhMWFRUWGBUYFhYVFxUVFRUVFRUYFxUXFxYYHSggGBolHRgXIjEhJSkrLi4uFx8zODMtNygtLisBCgoKDg0OGhAQGy0mICUtNS0rLy8tLS8tLS0tLS0tLS0tLS0tLS0tLS0tLS0tKy0tLS0tLS0tLS0tLS0tLS0tLf/AABEIAM0A9gMBIgACEQEDEQH/xAAcAAABBQEBAQAAAAAAAAAAAAAAAwQFBgcCCAH/xABKEAABAwIDBAcEBwQHBgcAAAABAAIDBBESITEFBkFRBxMiYXGBkTKhscEUI0JScoLwYpKy0TNDU3ODouEIFSQ1RJMWNGOzwtLx/8QAGQEBAAMBAQAAAAAAAAAAAAAAAAIDBAUB/8QALREAAgIBBAECBgEEAwAAAAAAAAECEQMEEiExQSJxExQyUWGB8DNSwdEVI0L/2gAMAwEAAhEDEQA/ANxQhCAEIQgBCEIAQhCAEIQgBCEIAQhCAEIQgBZ7WdK9M17mRwyvwktLjhY0lpsbZk28loS88bY3F2rHNKWUrXsL3ua9r8eIOcSLNHaBtzCtxfDv1lOb4lf9Zojekou9mADxefk1J0vSDPI/CIYwPF5PxWT1dNtSIi9JLEz7T5InEN5kkZAJajnrWnE2aMf4YPxK2Rjhkntjf89zDOWeLW+Vfz2N5i26/DdzWDwv/NRG0t9nx6MYfHF/NZNW7d2mcZZMzAw4TdjCb2B5ciPRVmr3mrCbOkafyAfBU7ILmSL985cRZr8/SzKzWmjd4Pc35Fcs6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m+L2j3XVc3h3ipy9retFg2+jrEu8uQHqqH/vaYZF58HAfML63az+OA+McZ+S6ENKou7OZk1cpKqIuuOTPA/wARTUNLrADWw9VY3bTxe1HEeXYt8F02ojOsLP8AMPmr3it3ZnWbaqouFC+NkbI2ubZrQBYjgEs8tcLXBVD2jVtbH9XG1pLgCfaysTx0zsoR20Xj7RHhkpOcYOiKxymrQ9rt1pGxzzgtwQymN4ucQuW4Ta2lnNzUVDM4MdEHNLHEE3FyHDiDqFrnRRs5/wBEfM6566QkXzu1gDb/AL2L0Ct8mzWH2omHxY0/JcyU0pPg68cbcVyeeaSmYDc9o9+norBQvWuP3ep3a08R/wANv8l8bunTHSnjHgLfBI5YrwRlhk/JmbhiFjmoU7xVVK98cUrmNBNmgnCL/aA0uea2pu5dOf6q3g54+aRd0Z0T343wlxyGb5LZdwKseeLVEI6aSdmabk73VstdBFJO5zHl4c02N7RPcOF9QFrwkKV2ZuVSQEOip4mOGjg0Yhlb2jmppmzAs0pWzXGNKiOpqxwUjS1Rcl2ULRwSzIQFAkKBC+oQAhCEAIQhACEIQAhCEAIQhACEIQAuDGF2hAIPpGHVoPiAU0l2FTu9qGM+LGn5KSQvbPKICXc6idrSw/uNHwTOXo92e7/pmj8Lnt+Dla0KSySXTZF44PtIpjujLZx1hcRrbrZbfxKRo9x9nxexSQg8ywPPq65ViQvHOT7YjCMekN4qNjQGtaGgaAAADwAXRpm8kshRJjcUreSUEISiEByGBfbL6hACEIQAhCEAIQhAf//Z
    (totally hijacking this thread in an awesome way)
     
  20. Trinary

    Trinary

    Joined:
    Jul 26, 2013
    Posts:
    395
    I do get asked that a lot. I'm thinking of making a video going over that this weekend.

    yield return was created by the .net foundation in order to make foreach statements work. It's designed to return a list of elements one at a time. As soon as you use yield return in a function that returns an IEnumerator type then .net recognizes that and compiles the function differently. This different structure allows you to return a value to some code that will do something with that value and then pass control back to the IEnumerator function, which resumes executing where it left off.

    Coroutines take advantage of this unique property of being able to return multiple values and resume where they left off each time. So yield return, which was originally designed to return a value in the list, is being used instead to pass control back to the process pump. When the process pump later calls the IEnumerator function that function will resume executing where it left off. This makes it really intuitive to structure a function that will execute over multiple frames. One of the only downsides is that we have to use commands (like yield return or yield break) that don't seem very well named, since the whole structure was created for a completely different purpose.
     
    laurentlavigne and Alverik like this.
  21. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    By the way, I'm very new to coroutines and I recently learned about WaitForSeconds. Previously, in my mobile project I was using InvokeRepeating to deplete a few stat meters/bars every some specified amount of time (similar to the sims), but I hear people say coroutines are better performant, so, should I change them to coroutines instead? I found this in a forum:

    Code (CSharp):
    1. StartCoroutine("Shoot");
    2.  
    3. IEnumerator Shoot() {
    4.      yield return new WaitForSeconds(5f);
    5.  
    6.      while (true) {
    7.          // shoot stuff
    8.          yield return new WaitForSeconds(2f);
    9.      }
    10. }

    The only thing is, I also have some code that cancels the invoke and restarts it in case Update() detects the time float gVar has been changed (which is meant to change the time on the fly)... but I guess, in the case that I change to coroutines, it would kind of be best if I capture the data in a float variable which I can then pass to the coroutine? or capture it directly inside the coroutine instead of update? (i'm guessing the second?) Anyway, my only worry is, If I change to coroutines won't there be a small delay when you change the time on the fly? (like using an special item, which for an amount of time will make your character get hungry more slowly) .

    Or should I just leave them as is? I haven't really profiled them, but they're just removing 1 from a 100 every X time, then updating a gVar tied to a meter/bar.
     
  22. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    How does this get around platforms that do not support multithreading, like WebGL?
     
    Alverik likes this.
  23. Trinary

    Trinary

    Joined:
    Jul 26, 2013
    Posts:
    395
    Ok, this discussion is now way outside of the scope of this thread. I just posted here to make sure that my coroutine asset wasn't being misrepresented.

    @Alverik If you want me to answer your question in depth then please ask in my coroutine thread. The short answer is that Update gets run every frame, and even an empty update function has a non-zero cost, whereas WaitForSeconds (Unity) or Timing.WaitForSeconds (MEC) won't call your coroutine at all until the time has passed, so that's how it's faster for your case.
     
    Alverik likes this.
  24. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    Sorry, I nearly forgot about it, because almost all the question had to do a bit with coroutines (and everyone here seems very savvy about them ;)).
     
  25. rickomax

    rickomax

    Joined:
    Jun 11, 2013
    Posts:
    683
    Isn't the following code suppose to throw an error?

    Code (CSharp):
    1. void Start () {
    2.    this.StartCoroutineAsync(MyCoroutine());
    3. }
    4. IEnumerator MyCoroutine() {
    5.   yield return Method();
    6. }
    7. IEnumerator Method() {
    8.   yield return Ninja.JumpToUnity;
    9.   yield return Ninja.JumpBack;
    10.   var x = this.transform; //should throw an error, but it will not, meaning it's back to Unity main thread
    11. }
     
    validname1 likes this.
  26. saravananparanthaman

    saravananparanthaman

    Joined:
    Apr 12, 2018
    Posts:
    3
    hi,

    unity version : 2017.4.11f1
    visual studio : Microsoft Visual Studio C# 2017
    platform : oculus go (Android Mobile/VR)

    My Code:

    Code (CSharp):
    1.  private IEnumerator SaveAsset(byte[] bytes, FileType type)
    2.         {
    3.             yield return Ninja.JumpBack;
    4.  
    5.             if (!File.Exists(pathDirectory))
    6.                 Directory.CreateDirectory(pathDirectory);
    7.  
    8.             string filePath = pathDirectory + "/"  + type;
    9.             using (FileStream fs = File.Create(filePath))
    10.             {
    11.                 fs.Write(bytes, 0, bytes.Length);
    12.             }
    13.             yield return new WaitForSeconds(0.1f);
    14.             yield return Ninja.JumpToUnity;
    15.  
    16.         }
    There isnt any lag on download code previous to this. After the download completion, there is lag on saving the asset to device. Anyway I inserted the above changes on the code, but doesnt seems to show any changes.

    Please help!!