Get ID of executing Process/Thread in C++ Builder

1.6k views Asked by At

Let's say I have a class with the function

class foo 
{
  ...

  void bar() {
    OutputDebugString(........);
     // mode code
  }
}

Is it possible to print the ID of the current thread (or if it's the main application) that is executing the function using OutputDebugString?

I have a large application I'm debugging and have found a deadlock situation and would like to check which threads are included in the deadlock. Since it could possibly be the same thread that is locking it's own critical section.

2

There are 2 answers

1
aioobe On BEST ANSWER

Have a look at the GetCurrentThread function.

0
Remy Lebeau On

Use GetCurrentThreadId().

Note that a thread cannot deadlock itself on a critical section. Once a thread has obtained the lock to the critical section, it can freeing re-enter that same lock as much as it wants (same thing with a mutex). Just make sure to unlock the critical section for each successful lock (re)entry so that OTHER threads do not become deadlocked.