- Home /
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)?
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.)
Sounds great to me. I wasn't sure. Took out the coroutine part just to be not-confusing.
Excellent - thanks. I've used Coroutines in $$anonymous$$ono 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. $$anonymous$$uch thanks. =)
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 :).
Answer by Unitraxx · Apr 15, 2013 at 02:53 PM
It will not cause an issue, it can however cause unexpected behavior.
Thanks Unitraxx, definitely appreciate the speedy response :) (I tried voting up your question but unable to do that yet :S ).
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)
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
![](https://koobas.hobune.stream/wayback/20220613100345im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make a var "global" 0 Answers
Prefab's Script affecting all the Prefab 1 Answer
Static, yet Private, Variables? 1 Answer
Trivial question for Javascript/Unityscript experts: static variables? 4 Answers
Best way to share a variable 1 Answer