Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mrtkhosravi · Jan 20, 2016 at 03:08 AM · multithreading

Need strategy for multithreading in unity

My unity project is a procedural environment on Android and creates terrains and stuff at runtime. The overall workflow I use is to calculate anything non-unity in a worker thread and when the data is calculated, I call Unity API within the main thread.

The problem is that sometimes (like every 200 frames) the worker thread affects the main thread's performance. That could show itself with a nasty spike in rendering time.

So what to do with multithreading in unity?

PS: Not exactly the main issue but to make the question more concrete, I will include the worker thread implementation. It is a modified version of the worker thread implementation suggested in this stackoverflow post. The modified version is like this:

 public static class JobScheduler
 {
 private static Queue<Job> Jobs = new Queue<Job>();
 private static volatile bool isBusy;
 private static ManualResetEvent _workAvailable = new ManualResetEvent(false);
 static JobScheduler()
 {
     var backgroundWorkThread = new Thread(BackgroundThread)
     {
         IsBackground = true,
         Priority = ThreadPriority.Lowest,
         Name = "BasicBackgroundWorker Thread"
     };
     backgroundWorkThread.Start();
 }
 
 private static void BackgroundThread()
 {
     int jobCnt;
     while (true)
     {
         Job? workItem=null;
         lock (Jobs)
         {
             jobCnt = Jobs.Count;
             if (jobCnt != 0 && !isBusy)
             {
                 workItem = Jobs.Dequeue();
             }
         }
         if (workItem!=null)
         {
             isBusy = true;
             workItem.Value.callback(workItem.Value.param);
         }
         else
         {
             _workAvailable.WaitOne();
             _workAvailable.Reset();
         }
     }
 }
 public static void AddJob(Job Job)
 {
     lock (Jobs)
     {
             Jobs.Enqueue(Job);
     }
         _workAvailable.Set();
 }
 
 public static void JobDone()
 {
     isBusy = false;
     _workAvailable.Set();
 }
 
 }


And the Job struct is:

 public struct Job
 {
 public object param;
 public WaitCallback callback;
 public Job(WaitCallback callBackP
     , object parameter)
 {
     callback = callBackP;
     param = parameter;
 }
 }

Whenever needed I call JobScheduler.AddJob to enqueue a job and call JobScheduler.JobDone after the job is done to allow the next job to run.

Also the ThreadPool is not an option since it produces unnecessary garbage and is not very flexible to use.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image incorrect · Jan 20, 2016 at 03:58 AM 0
Share

Have you checked Profiler? Does it gives any ideas what causes those spikes? $$anonymous$$aybe the cause of an issue is passing data from one thread to another?

avatar image mrtkhosravi incorrect · Jan 20, 2016 at 08:16 AM 0
Share

When the spike happens sometimes culling would take a long time. Sometimes my own scripts take huge time, my code is exactly the same every frame so I'm sure nothing fancy is happening in my code and no GC.Collect is happening. When I disabe the background worker the spikes go away

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Using BackgroundWorker Class let Unity crash. 2 Answers

Multithreading, Waiting for threads to complete. 0 Answers

Multithreaded Input Detection 2 Answers

Unity Multithreading only works partial 0 Answers

Getting texture size off main thread 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges