如何在 MacOSX 中设置线程名

2022-01-20 00:00:00 multithreading gdb c++ xcode

在 Windows 中,可以通过 此代码.然后线程名会显示在调试器中.

In Windows, it is possible to set the threadname via this code. The threadname is then shown in debuggers.

在 MacOSX 中,我看到了一些提示,表明存在线程名.我认为类 NSThread 也有一个名称属性.我的目标是我可以在我的 C++ 应用程序中设置线程名并在 Xcode/gdb 中查看它.

In MacOSX, I have seen several hints which indicates that there are threadnames. I think the class NSThread also has a name-attribute. My goal is that I can set the threadname in my C++ application and see it in Xcode/gdb.

其他相关问题:

  • 我可以设置一个名称吗pthreads/linux 中的线程?(对于 pthread 这里有一个很好的答案/概述)
  • 如何在 Linux 中命名线程?
  • 如何将名称设置为 Win32 线程?(同样有趣的是 这个Bruce Dawson 的讨论)
  • (Android) 如何为线程设置名称?
  • Can I set the name of a thread in pthreads / linux? (with a very good answer/overview for pthread here)
  • How to name a thread in Linux?
  • How to set name to a Win32 Thread? (also interesting is this discussion by Bruce Dawson)
  • (Android) How to set name to the thread?

推荐答案

我推荐如下:

[[NSThread currentThread] setName:@"My thread name"]; // For Cocoa  
pthread_setname_np("My thread name"); // For GDB.

(您需要包含 pthread.h)在 XCode 3.2.3 中工作(至少对于 iPhone 开发)

(You'll need to include pthread.h) Works a treat in XCode 3.2.3 (at least for iPhone development)

相关文章