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 UnLogick · Jun 10, 2011 at 05:11 AM · threadsthreading

Invoking main thread

I've seen alot of posts on how the Unity3d engine isn't thread safe. However very few posts on how you can safely use multi-threading within Unity3d. If the engine wasn't designed for multi-threading adding the current check is the only right thing to do. However that doesn't mean threading isn't supported! Or isn't safe. It just means that Unity3d won't try to handle all the weird race conditions that threading involves.

I'm currently making a small MMO (what an oxymoron I know!) never the less I feel I've narrowed the project enough that its not a monomental achievement. Blog with project outline

Now let me make it clear. I'm not using multi-threading cause its cool. Or because I have huge calculations. I use it cause most actions needs to swing by a server to be validated and/or randomized! I'm using the ASP.Net client generated by mono's wsdl and it has two modes, blocking and async. I'm going for async and that means I get my replies in a different thread.

First I looked for methods to invoke back to main thread, but found none. Even more so I found several posts saying I'm a dummy cause I insist on using threads. :)

Well luckily I previously did an advanced CommandQueue system. It works a bit like co-routines, it takes a bit longer to define you commands, however its slightly better suited for scripted animations and such. But the main advantage ofcourse is that it allows me to add commands in a thread-safe way and then execute them in main thread.

So my two cents to all with a similar problem. Make an abstract object with an abstract run() method. Then derive an object from that which holds the needed context information (perhaps a delegate to invoke) add it to a thread-safe list. You can then run through the list on Update() and voila!

Alternate/Simpler solutions:

  • PragmaScript suggests a list of actions. Word of warning, his sample keeps the list locked while the actions are executing. When a project grows big enough someone will cause a deadlock on that.

  • GabrielH suggests adding coroutines to a list and then starting the coroutine from main-thread.

  • If you just want a quick fix take PragmaScript's solution cause it has source code. Though I'd recommend swapping the list with an empty one. Then you can release the lock and call the actions without risking thread-locking issues.

Since I'm new with Unity3d I hope someone can point me towards the new feature suggestions page. Cause an InvokedCoroutine that calls the coroutine on main-thread would certainly solve most user problems.

Comment
Add comment · Show 5
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 jonas-echterhoff ♦♦ · Jun 10, 2011 at 07:13 AM 0
Share

Uhm. Is this actually a question? If not, please use our forums ins$$anonymous$$d. FWIW, features can be suggested at feedback.unity3d.com.

avatar image UnLogick · Jun 10, 2011 at 09:26 AM 0
Share

While not exactly a question, it represents some knowledge I looked for without success in this section earlier.

avatar image jonas-echterhoff ♦♦ · Jun 10, 2011 at 09:31 AM 0
Share

It is fine and appreciated to add knowledge to this site to share it with other people. However, to keep this useful, please stick to the Q & A format. Just post a question, and write your own reply (which you can then later accept). That way people searching for information will more likely look at your question, seeing that it has an accepted reply, and people looking to help other people will be able to skip reading it, seeing that it alread has an answer. Thanks, jonas.

avatar image UnLogick · Jun 11, 2011 at 08:28 PM 0
Share

sure thing. I'll do that next time. :o)

avatar image UnLogick · Jun 11, 2011 at 08:58 PM 0
Share

btw what forum would you put this into? Seems there is no knowledge base forum. The closest thing I came was the scripting forum and to be honest I don't feel thats the right forum. Welcome message second line: "You couldn't script it, could you? Well, maybe if..."

If you need a language tutorial, then I'd wait another month before starting on threads.

4 Replies

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

Answer by UnLogick · Jun 11, 2011 at 08:35 PM

I accidentally wrote the answer in the question.

So my two cents to all with a similar problem. Make an abstract object with an abstract run() method. Then derive an object from that which holds the needed context information (perhaps a delegate to invoke) add it to a thread-safe list. You can then run through the list on Update() and voila!

Alternate/Simpler solutions:

  • PragmaScript suggests a list of actions. Word of warning, his sample keeps the list locked while the actions are executing. When a project grows big enough someone will cause a deadlock on that.

  • GabrielH suggests adding coroutines to a list and then starting the coroutine from main-thread.

  • If you just want a quick fix take PragmaScript's solution cause it has source code.Though I'd recommend swapping the list with an empty one. Then you can release the lock fast such reducing the chances of a thread halting and waiting.

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
1

Answer by pdwitte · Apr 02, 2016 at 05:24 AM

Hey guys,

I created a simple class for this reason that you can call with a one-liner.

You can use it like this:

 public IEnumerator ThisWillBeExecutedOnTheMainThread() {
     Debug.Log ("This is executed from the main thread");
     yield return null;
 }
 public void ExampleMainThreadCall() {
     UnityMainThreadDispatcher.Instance().Enqueue(ThisWillBeExecutedOnTheMainThread());
 }

Simply head over to https://github.com/PimDeWitte/UnityMainThreadDispatcher and start using it if you'd like.

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 PaulUsul · Jun 10, 2011 at 10:07 AM

Since I'm new with Unity3d I hope someone can point me towards the new feature suggestions page

Only question I could find :)

http://feedback.unity3d.com/forums/15792-unity

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 Tom 17 · Jun 10, 2011 at 10:17 AM

Invoking the main thread is indeed a peculiar way to call what you are up to. Here is my two cents in the best hope that this helps you.

So whatever the layout of your threading may be, I will focus on the point where you need to synch anything that calls Unity-API with the main thread. Coroutines aren't really a seperate thread, they are executed on the main thread just as well. But you can yield their execution and wait for the end of the current frame (WaitForEndOfFrame). As I see it that's the place where you should synch. The way I would go is to have a script that starts a coroutine which yields for the end of a frame and invoke a delegate. this delegate could be filled with actions/task, whatever you will call it, by seperate threads. This way you can have an asynchronous thread that handles stuff that needs to be authorized by the server and you feed the results back in the game loop by adding some methods to the delegate.

This is not a complete system, but I hope it gets you started.

Comment
Add comment · Show 2 · 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 UnLogick · Jun 11, 2011 at 08:26 PM 0
Share

Hey $$anonymous$$

Well its called invoke in WinForms. You take any given System.Windows.Forms.Control and use the Invoke(Delegate) method to have it call a delegate from its thread. How fast the delegate is called depends on when that thread reaches its message loop. In unity that could be once every frame which is perfect for my needs.

Seems to me that WaitForEndOfFrame is a coroutine method used to plan changes that will take effect after this frame, or to grab screenshots. If it works outside of the main thread(unlikely) all it does is alter the odds of the thread lock collision. But while that might work on most systems, it could make a collision 99% of the time on another system.

avatar image UnLogick · Jun 11, 2011 at 08:49 PM 0
Share

Oh I almost forgot, you can't create a co-routine from a thread. So in order to use the co-routine I'd have to first sync up. The only advantage of a co-routine after sync is that it gives more flexibility than a delegate.

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

6 People are following this question.

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

Related Questions

Efficiency of coroutine and what it does? 2 Answers

How to warm-up job threads 0 Answers

[Problem] Use Playerpref.GetInt in other thread 0 Answers

Can I create a looping timer without separate threads? 1 Answer

How do i convert a 3dimensional array to be usable in a Job? 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