Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. thread ID

thread ID

Scheduled Pinned Locked Moved C#
csharpc++question
20 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    George_George
    wrote on last edited by
    #1

    Hello everyone, I am migrating from C++ to C#. And I am debugging a multi-threaded program and I need to print out the thread ID. In C++, I am using GetCurrentThreadID method, but in C#, I only find Thread.CurrentThread.ManagedThreadId. My questions are, 1. Does Thread.CurrentThread.ManagedThreadId have the same effect and can serve my purpose? 2. I am confused about what means "ManagedThreadId", does there is some peer called "NonManagedThreadId"? thanks in advance, George

    L 1 Reply Last reply
    0
    • G George_George

      Hello everyone, I am migrating from C++ to C#. And I am debugging a multi-threaded program and I need to print out the thread ID. In C++, I am using GetCurrentThreadID method, but in C#, I only find Thread.CurrentThread.ManagedThreadId. My questions are, 1. Does Thread.CurrentThread.ManagedThreadId have the same effect and can serve my purpose? 2. I am confused about what means "ManagedThreadId", does there is some peer called "NonManagedThreadId"? thanks in advance, George

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi George, 1. no 2. no there is no link between managed threads and kernel threads. managed threads are logical things, kernel threads are physical things. even if it seems there is a one-to-one correspondence, .NET does not promise that; in fact they claim multiple managed threads could be executed by a smaller number of kernel thread. As a consequence, there is simply NO WAY to identify your threads; not by name (Windows does not know about thread names), and not easily by thread IDs, they are confusing at the least. What you can do is catch the managed thread ID when the thread gets created, attach a thread name to it (using a Hashtable/Dictionary), then keep track of your threads by asking them their ID, and look up the thread name you assigned earlier. But that is a rather clumsy workaround for a lernel shortcoming. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Voting for dummies? No thanks. X|


      G 2 Replies Last reply
      0
      • L Luc Pattyn

        Hi George, 1. no 2. no there is no link between managed threads and kernel threads. managed threads are logical things, kernel threads are physical things. even if it seems there is a one-to-one correspondence, .NET does not promise that; in fact they claim multiple managed threads could be executed by a smaller number of kernel thread. As a consequence, there is simply NO WAY to identify your threads; not by name (Windows does not know about thread names), and not easily by thread IDs, they are confusing at the least. What you can do is catch the managed thread ID when the thread gets created, attach a thread name to it (using a Hashtable/Dictionary), then keep track of your threads by asking them their ID, and look up the thread name you assigned earlier. But that is a rather clumsy workaround for a lernel shortcoming. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Voting for dummies? No thanks. X|


        G Offline
        G Offline
        George_George
        wrote on last edited by
        #3

        Thanks Luc, 1. My application is pure C# application, and I want to dump thread ID to detect the behavior of each thread, so that I can find deadlock/re-entrance/how many times a thread enters a specific method, something like this. After reading document, I am confused whether I can use ManagedThreadId property, my concern is for example, whether two threads will map to the same ManagedThreadId, so that the monitor is not accurate to serve my purpose. 2.

        Luc Pattyn wrote:

        As a consequence, there is simply NO WAY to identify your threads; not by name (Windows does not know about thread names), and not easily by thread IDs, they are confusing at the least.

        Why not can not be identified by ManagedThreadId? Could you show me a scenario please? :-) 3.

        Luc Pattyn wrote:

        What you can do is catch the managed thread ID when the thread gets created, attach a thread name to it (using a Hashtable/Dictionary), then keep track of your threads by asking them their ID, and look up the thread name you assigned earlier. But that is a rather clumsy workaround for a lernel shortcoming.

        I am confused why ManagedThreadId is not enough? Could you show me a scneario when just using ManagedThreadId is not enough please? regards, George

        A L 3 Replies Last reply
        0
        • G George_George

          Thanks Luc, 1. My application is pure C# application, and I want to dump thread ID to detect the behavior of each thread, so that I can find deadlock/re-entrance/how many times a thread enters a specific method, something like this. After reading document, I am confused whether I can use ManagedThreadId property, my concern is for example, whether two threads will map to the same ManagedThreadId, so that the monitor is not accurate to serve my purpose. 2.

          Luc Pattyn wrote:

          As a consequence, there is simply NO WAY to identify your threads; not by name (Windows does not know about thread names), and not easily by thread IDs, they are confusing at the least.

          Why not can not be identified by ManagedThreadId? Could you show me a scenario please? :-) 3.

          Luc Pattyn wrote:

          What you can do is catch the managed thread ID when the thread gets created, attach a thread name to it (using a Hashtable/Dictionary), then keep track of your threads by asking them their ID, and look up the thread name you assigned earlier. But that is a rather clumsy workaround for a lernel shortcoming.

          I am confused why ManagedThreadId is not enough? Could you show me a scneario when just using ManagedThreadId is not enough please? regards, George

          A Offline
          A Offline
          anujarya_2001
          wrote on last edited by
          #4

          grfgsfdg

          G 1 Reply Last reply
          0
          • A anujarya_2001

            grfgsfdg

            G Offline
            G Offline
            George_George
            wrote on last edited by
            #5

            ?? regards, George

            1 Reply Last reply
            0
            • G George_George

              Thanks Luc, 1. My application is pure C# application, and I want to dump thread ID to detect the behavior of each thread, so that I can find deadlock/re-entrance/how many times a thread enters a specific method, something like this. After reading document, I am confused whether I can use ManagedThreadId property, my concern is for example, whether two threads will map to the same ManagedThreadId, so that the monitor is not accurate to serve my purpose. 2.

              Luc Pattyn wrote:

              As a consequence, there is simply NO WAY to identify your threads; not by name (Windows does not know about thread names), and not easily by thread IDs, they are confusing at the least.

              Why not can not be identified by ManagedThreadId? Could you show me a scenario please? :-) 3.

              Luc Pattyn wrote:

              What you can do is catch the managed thread ID when the thread gets created, attach a thread name to it (using a Hashtable/Dictionary), then keep track of your threads by asking them their ID, and look up the thread name you assigned earlier. But that is a rather clumsy workaround for a lernel shortcoming.

              I am confused why ManagedThreadId is not enough? Could you show me a scneario when just using ManagedThreadId is not enough please? regards, George

              A Offline
              A Offline
              anujarya_2001
              wrote on last edited by
              #6

              ryrt ruyeuttruuer e re njsdrfg irire ergtri tegr regtrre reirtirtgiers rtretrtgre

              G 1 Reply Last reply
              0
              • A anujarya_2001

                ryrt ruyeuttruuer e re njsdrfg irire ergtri tegr regtrre reirtirtgiers rtretrtgre

                G Offline
                G Offline
                George_George
                wrote on last edited by
                #7

                Can you speak English please? :-) regards, George

                1 Reply Last reply
                0
                • G George_George

                  Thanks Luc, 1. My application is pure C# application, and I want to dump thread ID to detect the behavior of each thread, so that I can find deadlock/re-entrance/how many times a thread enters a specific method, something like this. After reading document, I am confused whether I can use ManagedThreadId property, my concern is for example, whether two threads will map to the same ManagedThreadId, so that the monitor is not accurate to serve my purpose. 2.

                  Luc Pattyn wrote:

                  As a consequence, there is simply NO WAY to identify your threads; not by name (Windows does not know about thread names), and not easily by thread IDs, they are confusing at the least.

                  Why not can not be identified by ManagedThreadId? Could you show me a scenario please? :-) 3.

                  Luc Pattyn wrote:

                  What you can do is catch the managed thread ID when the thread gets created, attach a thread name to it (using a Hashtable/Dictionary), then keep track of your threads by asking them their ID, and look up the thread name you assigned earlier. But that is a rather clumsy workaround for a lernel shortcoming.

                  I am confused why ManagedThreadId is not enough? Could you show me a scneario when just using ManagedThreadId is not enough please? regards, George

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Hi George, ManagedThreadId is fine for identifying threads within .NET with two remarks: 1. it is a number, not a name; important stuff should be identified by name. 2. it does not map easily to the thread IDs as can be seen from other tools, such as Task Manager, SystemInfo, etc. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  Voting for dummies? No thanks. X|


                  G 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Hi George, ManagedThreadId is fine for identifying threads within .NET with two remarks: 1. it is a number, not a name; important stuff should be identified by name. 2. it does not map easily to the thread IDs as can be seen from other tools, such as Task Manager, SystemInfo, etc. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    Voting for dummies? No thanks. X|


                    G Offline
                    G Offline
                    George_George
                    wrote on last edited by
                    #9

                    Thanks Luc, 1.

                    Luc Pattyn wrote:

                    2. it does not map easily to the thread IDs as can be seen from other tools, such as Task Manager, SystemInfo, etc.

                    Because Task Manager will use a different ID? And ManagedThreadId is just CLR internal thread ID? 2. What do you mean this before? -------------------- As a consequence, there is simply NO WAY to identify your threads; not by name (Windows does not know about thread names), and not easily by thread IDs, they are confusing at the least. -------------------- Why no way? I think you mentioned in last post ManagedThreadId is ok to identify -- "ManagedThreadId is fine for identifying threads within .NET"? regards, George

                    L 1 Reply Last reply
                    0
                    • G George_George

                      Thanks Luc, 1.

                      Luc Pattyn wrote:

                      2. it does not map easily to the thread IDs as can be seen from other tools, such as Task Manager, SystemInfo, etc.

                      Because Task Manager will use a different ID? And ManagedThreadId is just CLR internal thread ID? 2. What do you mean this before? -------------------- As a consequence, there is simply NO WAY to identify your threads; not by name (Windows does not know about thread names), and not easily by thread IDs, they are confusing at the least. -------------------- Why no way? I think you mentioned in last post ManagedThreadId is ok to identify -- "ManagedThreadId is fine for identifying threads within .NET"? regards, George

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      Hi George, I found no easy way to relate ManagedThreadID inside a .NET app to the Thread IDs listed by kernel-level tools such as Task Manager, SystemInfo, etc. That makes it hard to observe multi-threading behavior unless you build a lot of code into the app itself. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      Voting for dummies? No thanks. X|


                      G 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        Hi George, I found no easy way to relate ManagedThreadID inside a .NET app to the Thread IDs listed by kernel-level tools such as Task Manager, SystemInfo, etc. That makes it hard to observe multi-threading behavior unless you build a lot of code into the app itself. :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        Voting for dummies? No thanks. X|


                        G Offline
                        G Offline
                        George_George
                        wrote on last edited by
                        #11

                        Thanks Luc, -------------------- As a consequence, there is simply NO WAY to identify your threads; not by name (Windows does not know about thread names), and not easily by thread IDs, they are confusing at the least. What you can do is catch the managed thread ID when the thread gets created, attach a thread name to it (using a Hashtable/Dictionary), then keep track of your threads by asking them their ID, and look up the thread name you assigned earlier. But that is a rather clumsy workaround for a lernel shortcoming. -------------------- So, you think the same is the same in TaskManager and in the .Net App -- even if the ID are not the same? And you use name to link the relationship between them? :-) regards, George

                        L 1 Reply Last reply
                        0
                        • G George_George

                          Thanks Luc, -------------------- As a consequence, there is simply NO WAY to identify your threads; not by name (Windows does not know about thread names), and not easily by thread IDs, they are confusing at the least. What you can do is catch the managed thread ID when the thread gets created, attach a thread name to it (using a Hashtable/Dictionary), then keep track of your threads by asking them their ID, and look up the thread name you assigned earlier. But that is a rather clumsy workaround for a lernel shortcoming. -------------------- So, you think the same is the same in TaskManager and in the .Net App -- even if the ID are not the same? And you use name to link the relationship between them? :-) regards, George

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          Hi George, no 1. I have no way to link managedThreadID (inside app) to kernel thread ID (tools outside app). 2. I use names to log threads, not IDs, to get a human readable log. :)

                          Luc Pattyn [Forum Guidelines] [My Articles]


                          Voting for dummies? No thanks. X|


                          G 1 Reply Last reply
                          0
                          • L Luc Pattyn

                            Hi George, no 1. I have no way to link managedThreadID (inside app) to kernel thread ID (tools outside app). 2. I use names to log threads, not IDs, to get a human readable log. :)

                            Luc Pattyn [Forum Guidelines] [My Articles]


                            Voting for dummies? No thanks. X|


                            G Offline
                            G Offline
                            George_George
                            wrote on last edited by
                            #13

                            Thanks Luc! Anyway, using ManagedThreadId is enough for my requirement, right? (I quote my requirement again here) -------------------- My application is pure C# application, and I want to dump thread ID to detect the behavior of each thread, so that I can find deadlock/re-entrance/how many times a thread enters a specific method, something like this. After reading document, I am confused whether I can use ManagedThreadId property, my concern is for example, whether two threads will map to the same ManagedThreadId, so that the monitor is not accurate to serve my purpose. -------------------- regards, George

                            L 1 Reply Last reply
                            0
                            • G George_George

                              Thanks Luc! Anyway, using ManagedThreadId is enough for my requirement, right? (I quote my requirement again here) -------------------- My application is pure C# application, and I want to dump thread ID to detect the behavior of each thread, so that I can find deadlock/re-entrance/how many times a thread enters a specific method, something like this. After reading document, I am confused whether I can use ManagedThreadId property, my concern is for example, whether two threads will map to the same ManagedThreadId, so that the monitor is not accurate to serve my purpose. -------------------- regards, George

                              L Offline
                              L Offline
                              Luc Pattyn
                              wrote on last edited by
                              #14

                              Hi George yes and different Threads will have different ManagedThreadId. :)

                              Luc Pattyn [Forum Guidelines] [My Articles]


                              Voting for dummies? No thanks. X|


                              G 1 Reply Last reply
                              0
                              • L Luc Pattyn

                                Hi George yes and different Threads will have different ManagedThreadId. :)

                                Luc Pattyn [Forum Guidelines] [My Articles]


                                Voting for dummies? No thanks. X|


                                G Offline
                                G Offline
                                George_George
                                wrote on last edited by
                                #15

                                Thanks Luc, Question answered. Always happy to learn from you. :-) regards, George

                                L 1 Reply Last reply
                                0
                                • L Luc Pattyn

                                  Hi George, 1. no 2. no there is no link between managed threads and kernel threads. managed threads are logical things, kernel threads are physical things. even if it seems there is a one-to-one correspondence, .NET does not promise that; in fact they claim multiple managed threads could be executed by a smaller number of kernel thread. As a consequence, there is simply NO WAY to identify your threads; not by name (Windows does not know about thread names), and not easily by thread IDs, they are confusing at the least. What you can do is catch the managed thread ID when the thread gets created, attach a thread name to it (using a Hashtable/Dictionary), then keep track of your threads by asking them their ID, and look up the thread name you assigned earlier. But that is a rather clumsy workaround for a lernel shortcoming. :)

                                  Luc Pattyn [Forum Guidelines] [My Articles]


                                  Voting for dummies? No thanks. X|


                                  G Offline
                                  G Offline
                                  George_George
                                  wrote on last edited by
                                  #16

                                  Hi Luc, Sorry for interruption again. I am thinking your thread ID and name matching solution. My concern is if you use thread pool to execute a task a couple of times, CLR may choose different thread to execute, which have different thread ID. How do you match in this situation -- to find thread name by thread ID? :-) regards, George

                                  L 1 Reply Last reply
                                  0
                                  • G George_George

                                    Thanks Luc, Question answered. Always happy to learn from you. :-) regards, George

                                    L Offline
                                    L Offline
                                    Luc Pattyn
                                    wrote on last edited by
                                    #17

                                    You're welcome. :)

                                    Luc Pattyn [Forum Guidelines] [My Articles]


                                    Voting for dummies? No thanks. X|


                                    G 1 Reply Last reply
                                    0
                                    • L Luc Pattyn

                                      You're welcome. :)

                                      Luc Pattyn [Forum Guidelines] [My Articles]


                                      Voting for dummies? No thanks. X|


                                      G Offline
                                      G Offline
                                      George_George
                                      wrote on last edited by
                                      #18

                                      I have an additional question here, http://www.codeproject.com/script/Forums/View.aspx?fid=1649&msg=2582539[^] regards, George

                                      1 Reply Last reply
                                      0
                                      • G George_George

                                        Hi Luc, Sorry for interruption again. I am thinking your thread ID and name matching solution. My concern is if you use thread pool to execute a task a couple of times, CLR may choose different thread to execute, which have different thread ID. How do you match in this situation -- to find thread name by thread ID? :-) regards, George

                                        L Offline
                                        L Offline
                                        Luc Pattyn
                                        wrote on last edited by
                                        #19

                                        Hi George, 1. I typically don't use ThreadPool because IMO it does not offer sufficient control. 2. If you were to use reusable threads (as in ThreadPool) you would have to remove the (ID,name) pair from the hashtable/dictionary as soon as the thread is done doing the action with that name. (You would have to lock the dictionary to do this properly!). :)

                                        Luc Pattyn [Forum Guidelines] [My Articles]


                                        Voting for dummies? No thanks. X|


                                        G 1 Reply Last reply
                                        0
                                        • L Luc Pattyn

                                          Hi George, 1. I typically don't use ThreadPool because IMO it does not offer sufficient control. 2. If you were to use reusable threads (as in ThreadPool) you would have to remove the (ID,name) pair from the hashtable/dictionary as soon as the thread is done doing the action with that name. (You would have to lock the dictionary to do this properly!). :)

                                          Luc Pattyn [Forum Guidelines] [My Articles]


                                          Voting for dummies? No thanks. X|


                                          G Offline
                                          G Offline
                                          George_George
                                          wrote on last edited by
                                          #20

                                          You are so smart, Luc! regards, George

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups