• 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
Question by Chowdery · Apr 15, 2013 at 02:29 PM · variablememorystatic

Variable access simulateneously

Quick question, I have some global static variables in my application and I'm wondering if it would be accidentally possible for 2 functions/objects to try accessing/modifying the same variable at the same time?

As in, Object 1 has it's own update script (which accesses/uses global variable), and Object 2 has it's own update script (which accesses/uses the same global variable). As Unity allows each object to be updated independently, doesn't that mean it's possible that both those objects might try access/modify the global variable at the same time (causing a memory access issue)?

Comment

People who like this

0 Show 2
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 Nercoe · Apr 15, 2013 at 03:00 PM 0
Share

Convert to an answer ^. Pretty much nailed it.

avatar image Unitraxx · Apr 15, 2013 at 03:11 PM 0
Share

There you go ;)

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Dracorat · Apr 15, 2013 at 08:49 PM

Update methods are actually run in sequence, not in parallel. Unity is only single-core on the logic threading portion of the application.

Unless you're doing some threading of your own, you're fine. However, if you start threads that reference data from another function, then you might want to start using locks. (short version: don't start your own threads.)

The basics to locking:

1) You create a lock object that derives directly from Object.

 // In the class somewhere, not inside a method
 Object lockObject = new Object();

2) You only use this object to get and release a lock. 3) You perform a lock through this:

 lock(lockObject){
     // Stuff here is locked
 }

4) You only "lock" around variable lookups or value sets. If you need a variable for more than a line or two, look up the value and store it to a local variable.

 var myLocalFloat = 0f;
 lock(lockObject){
     myLocalFloat = someOtherFloatThatHasLockingConcerns;
 }

5) You "lock" the variable everywhere it's used from now on.

6) You can use the same "lock" object for multiple items that share similar data. Use different locks for different data sets. Thus, locks should be class-scoped and not globally scoped.

7) Use getters and setters or explicitly implemented properties for any data that should be accessed or modified from a lock.

 public int WidgetCount {
     get { 
         int widgetCount = 0; 
         lock(objectLock) {  
             widgetCount = this.widgetCount; 
         }
         return widgetCount;
     }
 }

Therefore, keep locking to a minimum. Set up classes for threads before executing them and then don't mess with any data within the thread. Don't use threading unless you know what you're doing.

Note that if you get locking wrong, you could freeze your application while one lock waits for another that will never complete. (Such as locking and then using something that has the same object lock.)

Comment
Loius
Zadre

People who like this

2 Show 6 · 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 Dracorat · Apr 15, 2013 at 09:05 PM 0
Share

Sounds great to me. I wasn't sure. Took out the coroutine part just to be not-confusing.

avatar image whydoidoit · Apr 15, 2013 at 09:07 PM 1
Share

See:

http://unitygems.com/coroutines/

http://unitygems.com/advanced-coroutines/

http://unitygems.com/threads/

avatar image whydoidoit · Apr 15, 2013 at 09:07 PM 0
Share

Yeah - they are damn weird to start with :)

avatar image Dracorat · Apr 15, 2013 at 09:10 PM 0
Share

Excellent - thanks. I've used Coroutines in Mono before but of course you have to re-invoke each iteration when it's time for the next iteration yourself. I wasn't sure if Unity was doing that in parallel or not. The first article you linked has the flow laid out very clearly. Much thanks. =)

avatar image Chowdery · Apr 16, 2013 at 07:20 AM 0
Share

Thanks heaps Dracorat! Definitely cleared up everything for me, I've always been reluctant to access static variables for code within multiple objects, but no more! :). Also thanks "whydoidoit" for the resource links :).

Show more comments
avatar image

Answer by Unitraxx · Apr 15, 2013 at 02:53 PM

It will not cause an issue, it can however cause unexpected behavior.

Comment
Nercoe
whydoidoit

People who like this

2 Show 1 · 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 Chowdery · Apr 16, 2013 at 05:48 AM 0
Share

Thanks Unitraxx, definitely appreciate the speedy response :) (I tried voting up your question but unable to do that yet :S ).

avatar image

Answer by Aria-Lliane · Apr 15, 2013 at 08:30 PM

There are Synchronization methods both in C# (http://msdn.microsoft.com/en-us/library/ms173179.aspx) and in Java (http://stackoverflow.com/questions/6367885/java-synchronization)

Comment

People who like this

0 Show 1 · 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 whydoidoit · Apr 15, 2013 at 08:50 PM 0
Share

Unless you are starting your own threads (which can access nothing inside Unity core libraries in any case) there is no need to synchronise anything. The OPs problem may well be because they are overwriting values stored in a global variable - but no amount of sync would stop that as it isn't a thread contention issue.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

How Translation with a fils txt and script I18n ? 0 Answers

Static variables 1 Answer

Change static variable in JS through Csharp 1 Answer

Script effects all gameobjects. 1 Answer

Is there a simple replacement for a static variable? 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