Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Threading Chunks

Discussion in 'Scripting' started by sterynkng14, Aug 10, 2015.

  1. sterynkng14

    sterynkng14

    Joined:
    Sep 19, 2013
    Posts:
    36
    Okay, so I've spent way too much time on this, and I am getting to the point where it's going end up being pushed to the side for another time, but before that I'd like to ask you Unity Community, how would I go about threading this function and receive a return value from it, in this case the Chunk class.

    Code (CSharp):
    1. newChunk = terrainGenerator.ChunkGen(newChunk);
    Or is there a way I can thread the whole script itself? And just call it for it go to another thread.

    A little background: I am creating a procedurally generated voxel world and my chunks take for ever to load (noise calculating) but I read the best to enhance performance would be to have each chunk having a thread of it's own or something like that, and that in fact I should have been doing the entire time. I apologize if my question is vague or doesn't make since, been staring at these same lines of code for a couple hours trying different code.
     
  2. sterynkng14

    sterynkng14

    Joined:
    Sep 19, 2013
    Posts:
    36
    New to threading but I've done a little research on it.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Threads can't explicitly return a value. Its not like a regular function call. It happens asynchronously off in its own little world.

    What you can do is save data in some class level variable, then set a flag to alert the main thread the data is ready to read. In the unity context this normally means a coroutine that checks each frame if the task is done.
     
  4. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Or you could use events.
     
  5. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    I'd like to recommend using the Task Parallel Library backport that is included in the original versions of the reactive extensions package. Unfortunately, I don't have any experience using it in Unity so I'm not sure how it will perform.

    EDIT: I found this which might be lighter weight than the RxExtensions to get the TPL -- didn't try it myself though. https://www.nuget.org/packages/TaskParallelLibrary/

    The TPL makes threading quite a bit simpler so I think it is worth looking into. It uses an object design to encapsulate a lot of the details behind affinity, synchronization, cancelation, etc..

    If you're looking for pure speed, it may not be the way to go but it will definitely help you get multithreading working more easily. Perhaps someone has experimented with it in Unity and has some more expert comments..
     
    Last edited: Aug 10, 2015
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Will events work in a multithread environment? My understanding is events happen on the tread they are called from.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Most of the time threading in Unity is not so much about pure speed as it is about preventing blocking the main thread. The task library is a great fit for Unity, I've used it heavily in one of my projects. I haven't done threading stuff in a while, so I forgot about it. Inside Unity threading with tasks becomes as simple as:

    Code (CSharp):
    1. IEnumerator DoAsyncThing (){
    2.     Task myTask = Task.Run(SomeDelegate);
    3.     while (!myTask.isCompleted) yield return null;
    4.     // Do stuff after the task is done
    5. }
     
    eisenpony likes this.
  8. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    This is true if you interpret "event" with the C# implementation; event as a generic computing term still applies. The implementation of such a thing, however, would end up looking a lot like BoredMormon's original comment. Granted, it could be encapsulated a little better to trigger an event on the correct thread, which could then be subscribed to.

    This is precisely what the TPL is good at.
     
    Kiwasi likes this.
  9. sterynkng14

    sterynkng14

    Joined:
    Sep 19, 2013
    Posts:
    36
    Thanks for all the replies, I've seen tasks being used before by other people. I think i am going to go from there and give it a shot.
     
  10. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    You can create your own PoolEvents ; instance should be able to subscribe / unsubscribe to any event, they should be able to fire events from any thread and a manager must unstack events from the main thread.

    If you know how to use Dictionaries, locks and delegates, it's a piece of cake ;)