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 PsylentKnight · May 19, 2016 at 04:53 AM · c#global variableconcurrency

Are global variables thread safe?

Are global variables thread safe?

I have a global 2D array that keeps track of what tiles of the game are occupied. Many different scripts modify this array. Will I run into any race conditions?

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 Mikilo · May 19, 2016 at 08:33 AM

Hello,

Your global variable are not thread safe. But since Unity engine is mono-thread for all the game stuff, you don't need to worry about it.

BUT, if you do your own calculations on a thread different from the main thread, yes, you must be careful and start to lock and put fences everywhere it is required.

Remember only the main thread is allowed to touch Object!

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 MosoWare · May 19, 2016 at 05:14 AM

Unity3D methods are not thread-safe. However any 'static' field you make will be thread-safe after you use some of your System.Threading skills.

With this being said, using a static field as a wrapper for a Unity3D method may yield... unexpected results.

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 techmage · May 19, 2016 at 06:12 AM

'Atomic' means thread safe, see here: https://msdn.microsoft.com/en-us/library/aa691278(v=vs.71).aspx

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 jdean300 · May 19, 2016 at 08:41 AM

No they are not. You need to lock the array any time you read/write to it. This should do the trick:

 public class TileManager
 {
     private Tile[] m_Tiles;
 
     public Tile this[int i]
     {
         get { lock(m_Tiles) { return m_Tiles[i]; } }
         set { lock(m_Tiles) { m_Tiles[i] = value; } }
     }
 }

You also need to make sure you do not try to assign to the private array without a lock statement. Note that the getter does not lock the actual Tile object, only the array, so any operations on the Tile object will not be safe unless you lock it.

To address the problem mentioned in the comment below - if you are not creating your own threads, then you are safe and do not need to do any of this. Without concurrency there are not race conditions.

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 Mikilo · May 19, 2016 at 08:45 AM 0
Share

He is talking about scripts modifying the global array.

Since script are run from the main thread, locking is completely useless. Your advice is dangerous in the way is it a big waste of computation.

As far as I understand, he is not doing any multi-threading things.

avatar image jdean300 · May 19, 2016 at 08:49 AM 0
Share

If he is not multi-threading, then there are no race conditions - that is just a give in. A script could still create a separate thread to run a method that accesses the global array.

avatar image PsylentKnight · May 19, 2016 at 07:15 PM 0
Share

Sorry for the lack of clarity. I was asking about normal usage (since Unity uses multithreading internally). I am not creating any threads myself.

So to clarify, with normal usage I won't have any issues with race conditions when modifying global variables? What about if the modification is in a coroutine?

avatar image jdean300 PsylentKnight · May 19, 2016 at 07:17 PM 1
Share

As far as any code you write is concerned, Unity is not multi-threaded. It might be internally, but they have protected your code to the point that you do not need to worry about threading at all.

So, in short, you do not have to worry about race conditions, even if you are using coroutines.

avatar image Mikilo PsylentKnight · May 20, 2016 at 04:32 AM 1
Share

Unity is multi-threaded.

Not the game loop, which is run on the main thread only. They never put any protection since it is not intended to be run on other threads.

And as jdean300 said, coroutine is safe to use (Not thread-safe!).

Look at this asset, it uses coroutine, but in other threads. If you look carefully, there is methods to switch from the main thread and others. https://www.assetstore.unity3d.com/en/#!/content/15717

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

Global Variable Resseting By Itself 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