Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 capbob · Nov 17, 2013 at 06:50 AM · threadsthreadingprocedural-generationthreadgenerating

Thread ending prematurely?

I'm developing a game that generates procedural terrain in 32x32 chunks. To prevent slight freezes in the game as the player moves around I'm trying to move the terrain calculations to their own thread. Once that threads completes, the main Unity thread takes over and generates the terrain mesh. The code works fine when everything runs in the main thread aside from the slight freezes. When I move the terrain generation code to it's own thread however, sometimes I'll get a complete 32x32 chunk created but most of the time I only get a small piece of the chunk created. Is there something about threading that I'm not taking into account here?

Update The problem was the method that was creating the thread was ending before the thread could complete. I added "while (thread.IsAlive) Thread.Sleep(1)" to the code and that solved the problem however it introduced the problem of having the main thread sleep which defeats the purpose of what I'm trying to do. Guess I'll have to try a different route.

Comment
Add comment · Show 4
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 Eric5h5 · Nov 17, 2013 at 07:32 AM 0
Share

Impossible to say without code. Threads don't just stop for no reason though.

avatar image capbob · Nov 17, 2013 at 05:06 PM 0
Share

I figured it out though I still don't have a solution. See my update. Thanks.

avatar image supernat · Nov 17, 2013 at 05:19 PM 0
Share

First, tell us what parts of multithreading that you are taking into account. Second, how are you creating threads, using a real thread or a co-routine? See the following similar question to see if it helps. http://answers.unity3d.com/questions/357033/unity3d-and-c-coroutines-vs-threading.html

avatar image capbob · Nov 17, 2013 at 08:37 PM 0
Share

Thanks supernat! Using a ThreadPool was exactly what I needed.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by andrew-lukasik · Jun 21, 2017 at 02:30 PM

I had similar issue. It turned out that there was an exception but it wasn't printing to console due to not being main thread (throw-ing on worker thread manually will print nothing as well).

This is what helped - nesting your code inside try/catch block with Debug.LogException:

 Thread thread = new Thread(
     ()=> {
         try {
             /*
 
             Your actual code goes here
 
             */
         } catch (System.Exception ex) {
             Debug.LogException(ex);
         }
     }
 );


Comment
Add comment · Share
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
0

Answer by FlaSh-G · Jun 21, 2017 at 03:31 PM

You can easily solve this problem: Don't wait with Thread.Sleep, but by waiting frame after frame:

 void Update()
 {
   if(!workerThread.IsAlive)
   {
     DoAThing();
   }
 }

Or even better: Use a coroutine. Feel free to use this class of mine like this:

 void Start()
 {
   StartCoroutine(MyCoroutine());
 }

 IEnumerator MyCoroutine()
 {
   yield return new CoroutineThread(() =>
   {
     // Threaded stuff here
   });
   // Stuff after thread has finished here
 }
Comment
Add comment · Share
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

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What does new Thread do, and how many threads are too many? 1 Answer

will monobehaviour stay alive after DOTS? 0 Answers

Job System without using the main thread 1 Answer

LobbyHook doesn't give clients variables 0 Answers

How to prevent heavy method calculation from locking Unity up? 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