Search Unity

How to run code on parallel thread?

Discussion in 'Scripting' started by ScottySR, Dec 13, 2018.

  1. ScottySR

    ScottySR

    Joined:
    Sep 29, 2018
    Posts:
    49
    I am aware there are already some threads about this, but all I found and had code samples were uncommented, so I didn't really understand the code much. Many of the threads were years old so I don't think it'd be a good idea to reply to one of them.

    Anyway, how do I actually handle running the code? If you are giving me a sample code comment it and keep unnecessary code at minimum (instead of example code to what run in the thread, you could just add comment saying that the parallel thread's code goes there).
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    I suggest you look into quite new Job System.
    Is much safer approach, than anything else, from older days.
     
  3. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    JeffDUnity3D likes this.
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    They're still a tool that one shouldn't miss when developing with Unity. :)

    The real question is whether the OP really wants to write code that benefits from actual multi-threading, or whether he's been looking at coroutine examples with the common misconception that they're about parallelism, and just wonders how they work.
    From a plain code example, a method that's passed to a thread instance seems almost self-explanatory, whereas coroutines can look weird the first time you see them - perhaps that's what he's really asking about?

    @OP
    Can you add an example so that we can figure out whether you refer to real multi-threading?
     
  6. That's true but not in a thread which has this in the title: "How to run code on parallel thread?"
    A lot of under-experienced people think that Coroutines are "free" on the main thread. And they have a hard time to understand the time-sharing concept later.
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I mentioned it because there are hundreds if not thousands of threads where coroutines were thought to be a multi-threaded approach... There's a certain chance that it applies here as well, as explained earlier.

    I mean, take the initial post and read about the desired example... follow the instructions, keep the example minimal: in plain C#, you'll end up creating a thread and passing a delegate (or an implicitly converted method).

    Personally I don't think that's what he's looking for... but that's just my impression, noone knows.
     
    Lurking-Ninja likes this.