guillem
guillem

Reputation: 2998

CLR managed threads: lightweight vs heavy process

I'm working on an application that process pipelines in separate threads. During my tests I have seen that if a process is "lightweight" or the CLR determines that this is going to end quickly CLR recycle this thread rapidly and various units of work can share at the same time the same thread.

On the contrary if a process take's some time or has more load CLR open different threads.

To me all that difficult TLS Thread local storage programming.

In fact my application pipelines take some time to process and it seems that CLR is always assigning one managed thread for each other. BTW if in some case two pipelines share one managed thread they will collide because they use TLS variables.

After all that here comes the real question... Can I do the assumption that If a process takes some time/load it will always use it's own thread, or am I crazy doing that?

For what I have been reading managed threads in .net 3.5 is like acting with a kind of black box. So perhaps this question can never really be responded.

EDIT:

With process I am refereing to the dictionary definition A series of actions, changes, or functions bringing about a result an not the computer process you identify in task manager.

Upvotes: 1

Views: 592

Answers (2)

supercat
supercat

Reputation: 81247

Code run from a threadpool thread should not place anything in thread-local storage which it is not going to remove via finally block. If you need to ensure that any thread-local storage used by a piece of code will die after that code finishes executing, you need to explicitly either clean up the storage or run that code in its own thread.

Upvotes: 0

Tigran
Tigran

Reputation: 62286

Can I do the assumption that If a process takes some time/load it will always use it's own thread, or am I crazy doing that

Process always uses its own threads. It's not possible access other process's thread, not that I'm aware of.

Upvotes: 1

Related Questions