1

So, in the project I'm working on, I've been logging what thread ID functions are being executed on. Originally this started as a curiosity, but I've noticed that the thread ID steadily increases through the lifespan of the application. Not only that, but if left idle for a while, the next time a method get called, the thread ID will jump in value.

For Example,

Relay Event Invoked On Thread: 12

(0) Characters Left In Message Queue

Message Read On Thread: 12

Relay Event Invoked On Thread: 37

(0) Characters Left In Message Queue

Message Read On Thread: 37

Relay Event Invoked On Thread: 38

(0) Characters Left In Message Queue

Message Read On Thread: 38

Relay Event Invoked On Thread: 39

(0) Characters Left In Message Queue

Message Read On Thread: 39

This got me wondering how many threads were in use, so I started logging current threads, and the process threads always seem to be between twelve and eighteen.

The output looks like this:

Number Of Threads In Use: [16]

Worker Threads (available/max): [2045/2047]

Completion Port Threads (available/max): [1000/1000]

Now, the fact that the number of threads in use in this output has not gone beyond 18 makes me think they're not stacking up on each other.. but it also doesn't match the threads display in the debug window is VS.

enter image description here

So all this, plus info on SO, like this answer, that say once terminated, a thread ID can be reused, has me confused.

So, questions..

  1. Should I be worried about the increasing value in thread ID?
  2. What are some causes that could be preventing thread IDs from being reused?
  3. Could threads not displayed in the threads window when debugging be active? Could this be misinforming me?
  • 1
    Not so sure that the accepted answer there distinguishes between the operating system's TID and the ManagedThreadId. Not the same, a Thread in a managed program does not have to be an OS thread. Not that this happens in practice. So the CLR still has to pick a number to identify a managed thread, it simply increments the value for every new thread it creates. What you see back in the debugger's Threads window accurately represents the ones that are running, you just can't see the ones that are not running anymore. Nothing unusual. – Hans Passant Apr 05 '18 at 09:46
  • Cool, thanks Hans. You put my mind to ease. Now I feel like a worry-wart. It's much appreciated. Set it up as the answer if you want. Not that you need the rep :P –  Apr 05 '18 at 09:51

0 Answers0