Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ThreadStaticAttribute marked fields are automatically released when thread dies?

I discovered 'ThreadStaticAttribute', and I have a lot of questions about it: all my previous thread dependent static informations, were implemented as a static dictionary which TKey is Thread, and when I wanted to access it, I used Thread.CurrentThread and that works. But this requires mantainance, because if a thread dies, I have to delete the corresponding entry from the dictionary. And I also need to consider thread-safety, and a lot of other matters.

By using ThreadStaticAttribute, all these matters seems to be solved, but I need to be sure of it. My questions are: do I need to delete the instance hold by 'ThreadStaticAttribute' marked fields, somehow, before the thread dies?? Where is the information of that field hold?? It is in the instance of Thread object, or something like that, so that when it is not used anymore, garbage collector automatically discards it? Are there performance penalties? What ones? Is it faster than using a Keyed collection like I was doing?

Please, I need clarification on how 'ThreadStaticAttribute' work.

Thanks.

like image 303
Miguel Angelo Avatar asked Jan 15 '10 21:01

Miguel Angelo


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

No you do not need to delete instances of values help in a field which is tagged with ThreadStatic. The garbage collector will automatically pick them up when both the thread and the object are no longer reachable by rooted objects.

The only exception here is if the value implements IDisposable and you want to actively dispose of it. In general this is a hard problem to solve for a number of reasons. It's much simpler to not have values which implement IDisposable and are in a ThreadStatic field.

As to where this field is actually stored, it's somewhat irrelevant. All you need to be concerned about is that it will behave like any other object in .Net. The only two behavior differences are

  1. The field will reference a different value per accessing thread.
  2. The initializer for the field will only be run once (in practice, it's a bad idea to have any).
like image 51
JaredPar Avatar answered Oct 03 '22 20:10

JaredPar


Marking a static member variable as [ThreadStatic] tells the compiler to allocate it in the thread's memory area (eg. where the thread's stack is allocated) rather than in the global memory area. Thus, each thread will have its own copy (which are guaranteed to be initialized to the default value for that type, eg. null, 0, false, etc; do not use in-line initializers as they will only initialize it for one thread).

So, when the thread goes away, so does its memory area, releasing the reference. Of course if it's something that needs more immediate disposal (open file streams, etc) instead of waiting for background garbage collection, you might want to make sure you do that before the thread exits.

There could be a limit to the amount of [ThreadStatic] space available, but it should be sufficient for sane uses. It should be somewhat faster than accessing a keyed collection (and more easily thread-safe), and I think it's comparable to accessing a normal static variable.

Correction: I have since heard that accessing ThreadStatic variables is somewhat slower than accessing normal static variables. I'm not sure if it is even actually faster than accessing a keyed collection, but it does avoid issues of orphans (which was your question) and needing locking for threadsafety which would complicate a keyed-collection approach.

like image 40
Rob Parker Avatar answered Oct 03 '22 20:10

Rob Parker