简体   繁体   English

获取在C ++ Builder中执行进程/线程的ID

[英]Get ID of executing Process/Thread in C++ Builder

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 ? 是否可以使用OutputDebugString打印执行该函数的当前线程(或如果它是主应用程序)的ID?

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. 由于它可能是与锁定该线程相同的线程,因此它是自己的关键部分。

看一下GetCurrentThread函数。

Use GetCurrentThreadId() . 使用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. 只需确保为每个成功的锁定(重新)输入解锁关键部分,以免其他线程陷入死锁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM