- Home /
ThreadStatic attribute not crashing as the docs said
Hi, i wanted to make a thread safe static var using the ThreadStatic attribute, but the unity docs says that using that attribute will cause a crash. I tested making a simple script that uses it and i have not crashes at all, can somebody confirm that is working as expected?
Thanks!
Yeah tried it out too and have seen no crash. On Windows and in the Editor at least. $$anonymous$$aybe it has something to do with other platform problems? Or maybe it is causing crash when using the static var from different threads (haven't tried this one). As a reasoning I can think of maybe there is a possibility in .Net Fw to hook up into static var initialization or actions done per thread by this attribute and it spoils it in some way. I dunno.
"maybe [...] when using the static var from different threads". This sentence makes no sense. The point of ThreadStatic is that every thread actually has it's own variable. There is no way that another thread can access the content of that variable which belongs to a different thread. Every thread only "sees" it's own variable version. Though as i said you can't use static field initializers for thread static variables ()well you can but it's pointless / doesn't do what you might expect. It would only initialize the variable for the first thread that accesses the class. On all other threads the variable would be uninitialized.
So generally static variables of a primitive type / value type would start with their native default value. This can't be changed. The same applies to reference types which would always start with "null". For reference types you usually want to use lazy loading. Something like this:
[ThreadStatic]
private static $$anonymous$$yClass m_Obj;
public static $$anonymous$$yClass Obj
{
get
{
if (m_Obj == null)
m_Obj = new $$anonymous$$yClass();
return m_Obj;
}
}
In this case the "Obj" property will represent a thread static instance of $$anonymous$$yClass which is lazy initialized when used for the first time from any thread. $$anonymous$$eep in $$anonymous$$d that the property itself is not thread static. So the getter code is the same no matter from which thread it's called. However the "m_Obj" variable is thread static. So the same code uses a different variable depending on on which thread it's executed.
If you don't actually know what thread static is good for or how it works, chances are really high you don't need it or you don't want to use it. If you really think you need it, make sure you actually read the Remarks section of the documentation carefully.
Answer by Bunny83 · Apr 24, 2018 at 08:00 PM
I have no idea why they added this single-line note at the end. I've used it myself to create thread independent pools. I can't really come up with a good reasoning for that claim as this attribute only applies to static fields which Unity shouldn't mess with at all when it comes to serialization. It might cause problems with hot reloading during play mode though when using ThreadStatic you usually use lazy initialization anyways since the static constructor or field initializers can't be used here.
Your answer
Follow this Question
Related Questions
IJobParallelForTransform is not Multi threaded? 1 Answer
How could I center a GUI box on screen? 3 Answers
A weird issue in Unity related to scripts and Editor. 1 Answer
Smoothly move 2D Sprite 2 Answers