Search code examples
c#multithreadingbackgroundworker

Several background workers incrementing thread IDs rapidly


I'm making an app with about six background workers and one Thread with queue for mssql queries. It has to be very stable and development is towards the end, so I run a small test, where the app ran for about four hours. Everything was fine, except threads IDs.

I log all important events and error together with thread ID. The problem is, that when the app started, there was I*D values between 5 and 15* but after four hours most of thread ID values were between 70 and 90. Only sometimes those were 15, 40, or something smaller.

My question is can this lead to "running out of free threads" error? I don't know why is this happening. Two background workers make infinite loop (checking for value) and the others are run several times, when its needed. Is there some kind of GC for threads? Why the IDs have so big values?

I didn't do anything else on the machine while the test ran.


Solution

  • Assuming your workers are coming to a complete end, and a new one isn't being spun up while the old one is still in limbo, I wouldn't worry about the thread ID increasing, it just indicates a new thread being created - as long as the old ones complete, that's fine. You can connect your program to the Concurrency Analyzer and actually verify that behavior - see http://msdn.microsoft.com/en-us/magazine/ee336027.aspx