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
1
Question by jjobby · May 02, 2010 at 05:11 AM · threads

Can I use Unity math function in other threads?

I've read that Unity is not thread safe and I can only call Unity API in main thread. I'm trying to use multi-threading mainly for pure math calculation such as vector manipulation or something like that. I want to ask that can I use Unity basic math function in other thread such as vector magnitude, square root, dot/cross product etc? Or do I need to code those calculation functions too?

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

4 Replies

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

Answer by duck · May 02, 2010 at 04:53 PM

As far as I have heard, the official line is that the whole Unity API is not thread safe. Whether this applies to particular aspects such as the Mathf class, I'm not sure.

Personally, I can't see how a static math function which uses only variables defined within its own scope could be thread-unsafe, but that's just speculation without access to the actual source code.

However, since version 2.6, Unity has an assert which checks whether the the non-thread-safe functions are being called from an external thread. So, if you're doing something unsafe, you should get an assert fail in the console, looking something like this:

!Thread::EqualsCurrentThreadID(m_MainThreadID) 

If you're not getting that message, you may be safe.

If you are getting the warning, you might want to implement your own thread-safe versions of those particular functions (which are relatively simple, and many examples exist out there which you could copy).

Incidentally, Debug.Log is thread safe (I think this is the only Unity function where this has been specified). So you can still log results from your functions which are running in other threads!

Comment
Add comment · Show 3 · 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 Lucas Meijer 1 ♦♦ · May 02, 2010 at 10:28 PM 0
Share

duck is right..

avatar image fadden · Mar 24, 2016 at 09:44 PM 0
Share

FWIW, the current documentation says nothing about thread safety, and I can't find any page that claims Debug.Log is guaranteed to be safe.

avatar image Bunny83 fadden · Mar 24, 2016 at 10:43 PM 0
Share

The documentation has changed many times, however Debug.Log always has been thread-safe. That's why we have two different log callbacks Application.log$$anonymous$$essageReceivedThreaded and Application.log$$anonymous$$essageReceived.

Strangely those are currently missing in the member listing of the Application class ^^. They really have some issues with their documentation system.

The $$anonymous$$athf class is completely thread-safe as it's just a wrapper for System.$$anonymous$$ath. $$anonymous$$ost of Unity's built-in value types like Vector2/3/4, $$anonymous$$atrix4x4, Quaternion are also thread-safe.

avatar image
1

Answer by Eric5h5 · May 02, 2010 at 04:56 PM

System.Math is for sure thread-safe, though it doesn't have vector functions.

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 qJake · May 02, 2010 at 05:13 AM

There isn't any reason for me to believe that the Mathf function isn't thread-safe.

Did you try it? Something like this would be really easy to test, so why not just try it and see if it works instead of asking here? A better question to ask here is what alternatives you could use if it doesn't work.

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 duck ♦♦ · May 02, 2010 at 04:30 PM 1
Share

one of the problems with non-thread safe code is that they can appear to work fine 99% of the time, and it's possible for race-condition bugs can only show up extremely rarely by chance - so in cases like this it's best to know rather than just test :) (That said, I don't know the actual answer to the question!)

avatar image Facundo · May 28, 2010 at 05:02 PM 0
Share

Another reason to not trust thread-unsafe code is that it shows different behaviour depending on thread scheduling, the processes that are sharing the cpus and I/O tasks, which usually change from one machine to another. That makes impossible to get reliable testing on a single machine.

avatar image
0

Answer by StephanK · May 02, 2010 at 01:25 PM

Yes you can. What you can't (or shouldn't) do is manipulating the renderstate from a thread, as this probably will crash 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

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

2 People are following this question.

avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

play animation sequence 3 Answers

WorldToScreenPoint in Update/OnGUI: Bad Performance 1 Answer

Threads don't operate properly in every frame. 1 Answer

How to import the object from server to unity 2 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