获取 C++ 中正在执行的进程/线程的 ID建设者
假设我有一个带有该函数的类,
class foo
{
...
void bar() {
OutputDebugString(........);
// mode code
}
}
是否可以使用 OutputDebugString 打印正在执行该函数的当前线程(或者如果它是主应用程序)的 ID?
我正在调试一个大型应用程序,并且发现了死锁情况,并且想检查死锁中包含哪些线程。因为它可能是锁定它自己的关键部分的同一个线程。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看
GetCurrentThread
函数。Have a look at the
GetCurrentThread
function.使用
GetCurrentThreadId()
。请注意,线程不能在临界区上使自身死锁。一旦线程获得了关键部分的锁,它就可以根据需要释放重新进入同一锁(与互斥体相同)。只需确保每次成功锁定(重新)进入时解锁关键部分,以便其他线程不会陷入死锁。
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.