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 georgeq · Jun 18, 2014 at 03:24 PM · coroutine

Coroutine... should I worry about synchronization or not?

Currently I'm handling my game control as part of the OnTriggerEnter2D event, everything works fine, however the game looks slow at times, so in order to optimize performance I want to move such code to a co-routine, however I still will be creating, updating and destroying objects inside OnTriggerEnter2D as it is the natural place to do so...

I don't have enough knowledge or experience in Unity, so I have a big doubt here: should I worry about synchronization or not??? If the co-routine is called from the same thread as the OnTriggerEnter2D event, then I don't even need to worry about synchronization, however if different threads are involved I better provide a synchronization method otherwise things can go really really wrong.

Anyone with experience in co-routines can provide some light please?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Jun 18, 2014 at 03:27 PM

Coroutines run in the main thread, like most parts of scripting environment. So you don't have to worry about synchronization. The only things which might run in parallel are internal background things you usually don't have access to.

Comment
Add comment · Show 5 · 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 georgeq · Jun 18, 2014 at 04:28 PM 0
Share

Thanks, I do appreciate your answer. After several tests, it seem your statements are absolutely right, but just to be 100% sure, is there any official documentation I can refer to???.

avatar image Sharp-Development · Jun 18, 2014 at 06:53 PM 0
Share

http://docs.unity3d.com/$$anonymous$$anual/Coroutines.html && http://www.altdev.co/2011/07/07/unity3d-coroutines-in-detail/

avatar image georgeq · Jun 18, 2014 at 06:59 PM 0
Share

Thanks, I read both articles previously, but in none of them it is clearly stated that Coroutines run on the main thread, that's precisely the reason for my doubts, and that's why I posted this question.

avatar image Bunny83 · Jun 18, 2014 at 07:20 PM 0
Share

Coroutines are actually objects and not routines in the traditional sense. The "coroutine objects" implement cooperative multitasking, so they never run simultaneously with their calling code. The whole scripting environment of Unity runs on a single thread, that's why they don't care to make any API thread-safe. As I said, only a few internal processes might run on a seperate thread (on the native C++ side) but the whole managed world runs in the main thread.

There are a few exceptions now, like the new serialization callbacks which might be called from the loading thread as well as object constructors which you shouldn't use mainly for that reason.

However the coroutine scheduler definitely runs on the main thread. There are actually just a few points in the mainloop where the scheduler "schedules" a coroutine. Even asynchronous tasks like a WWW request are scheduled on the main thread so you never have to worry about threading.

You might want to read the coroutines++ and advanced coroutines articles on UnityGems to get a better understanding of how coroutines are implemented in Unity.

avatar image georgeq · Jun 20, 2014 at 05:44 PM 0
Share

Thanks, these two articles are really worth reading. I Appreciate it very much

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Fade object in smoothly not working 1 Answer

Multi-threaded / non-blocking way of encoding images to Texture2D 1 Answer

StopCoroutine() is not stopping my coroutines 1 Answer

Hold or Wait while Coroutine finishes 6 Answers

lerp in coroutine doesn't work correctly 0 Answers


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