views:

82

answers:

2

Main execution path (main thread) is going to be forked into two execution paths (two new threads on different jobs) but the main thread is no longer needed. I can assign one of the tasks to main thread and save one thread (one task by main thread and another by a new thread) but I was wondering putting main thread in an infinite sleep Thread.Sleep(Timeout.Infinite) is a good approach or not. My class is going to be instantiated many times and if a thread in infinite sleep takes resource from OS it's bad news for me.

A: 

All threads tie up resources, regardless of if they're sleeping or not.

Eric
+1  A: 

Each thread you create takes up stack space. On Windows, that's 1MB by default. There are also other internal house-keeping data structures that the operating system uses to keep track of threads which will take up a bit of memory as well, but the 1MB stack is definitely going to be the biggest consumer of resources.

Having said that, if we're only talking about 2 vs. 3 threads, then the difference is quite small. If it was 200 vs. 300 then you might have something to worry about. But if you're spawning a lot of threads, you'd be better off using some kind of thread pool (like, say, the one built-in to the .NET framework) rather than spawning individual threads anyway.

Dean Harding
Thanks. I cannot use pooling since it's an asp.net application.
Xaqron
@Xaqron: You can still use the thread pool in an ASP.NET application, why do you think you cannot? And how does `Thread.Sleep(Timeout.Infinite)` work in an ASP.NET application anyway? Where are you doing this "infinite" sleep?
Dean Harding
I read somewhere using thread pool in ASP.NET applications if you are going to have many threads (my scenario) makes ASP.NET starving because we all know it uses worker threads for handling requests. I use infinite loop at the beginning of an http handler and I'll describe the whole scenario in another question since anybody don't like long vague questions.
Xaqron