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 borro56 · Apr 24, 2018 at 06:08 PM · scripting problemthreads

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!

Comment
Add comment · 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 Romeno · Apr 04, 2019 at 06:37 PM 0
Share

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.

avatar image Bunny83 Romeno · Apr 05, 2019 at 04:53 AM 0
Share

"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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

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

144 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

IJobParallelForTransform is not Multi threaded? 1 Answer

How could I center a GUI box on screen? 3 Answers

I cant find out what will fix this code it has stumped me for a week now with no solution that I can find 2 Answers

A weird issue in Unity related to scripts and Editor. 1 Answer

Smoothly move 2D Sprite 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