macos Mac/iPhone:有没有办法在不使用 Objective-C 的情况下获取线程标识符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1540603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mac/iPhone: Is there a way to get a thread identifier without using Objective-C?
提问by Teemu Kurppa
Is there a way to get any kind of thread identifier of the currently running thread without resorting to Objective-C's NSThread.
有没有办法在不求助于 Objective-C 的 NSThread 的情况下获取当前正在运行的线程的任何类型的线程标识符。
I'm improving our custom debug tracing system to handle multiple threads properly. For each line of trace output, I'd like to print a thread id or a thread name. Threads are instantiated in various ways, e.g. both NSOperationQueue and pthread functions are used.
我正在改进我们的自定义调试跟踪系统以正确处理多个线程。对于每一行跟踪输出,我想打印一个线程 ID 或线程名称。线程以各种方式实例化,例如同时使用 NSOperationQueue 和 pthread 函数。
I've currently a following two alternatives, but I'm not satisfied with either of them. Are there any other options?
我目前有以下两种选择,但我对其中任何一种都不满意。还有其他选择吗?
pthread_t option
pthread_t 选项
pthread_t
seems to be a typedef of a platform specific _opaque_pthread_h
. It would be ok to use fields of _opaque_pthread_h
for now, even if it's hack-y and not portable. It has a __sig
field of type long, but that seems to have a same value for all threads of my process.
pthread_t
似乎是特定于平台的 typedef _opaque_pthread_h
。现在可以使用字段_opaque_pthread_h
,即使它是 hack-y 并且不可移植。它有一个__sig
long 类型的字段,但对于我进程的所有线程来说,它似乎具有相同的值。
NSThread option
NSThread 选项
[NSThread name]
requires NSAutoreleasePool
to be in place, but I don't want that to be a requirement as most of our code is pure C++, so it would be nice to just to launch c++ function without autorelease pool wrapping.
[NSThread name]
需要NSAutoreleasePool
到位,但我不希望这是一个要求,因为我们的大部分代码都是纯 C++,所以只启动 C++ 函数而不用自动释放池包装会很好。
回答by Teemu Kurppa
I found a one way that is enough to get some kind of unique identifier for trace output.
我找到了一种足以为跟踪输出获取某种唯一标识符的方法。
pthread_mach_thread_np
can be used to get a thread identifier, an unsigned int on iPhone.
pthread_mach_thread_np
可用于获取线程标识符,iPhone 上的 unsigned int。
mach_port_t tid = pthread_mach_thread_np(pthread_self());
Apparently this is a same thread id that is used in NSLog output.
显然,这与 NSLog 输出中使用的线程 ID 相同。
回答by Ken
See pthread_getname_np
.
见pthread_getname_np
。
Unfortunately NSThread's name is not currently pushed down to that. The NSThread name is just an ivar, so there will be no way to get at it except through the method. You could always make a C function that makes the autorelease pool and gets the name. Your C++ code then doesn't have to be compiled as ObjC++.
不幸的是,NSThread 的名字目前并没有被推到那个程度。NSThread 名称只是一个 ivar,因此除非通过方法,否则将无法获得它。您总是可以创建一个 C 函数来创建自动释放池并获取名称。然后您的 C++ 代码不必编译为 ObjC++。
pthread_getname_np
is probably a bit more useful than NSThread's name right now anyway. gdb and Instruments don't know about NSThread's name, only the pthread level name.
pthread_getname_np
无论如何,现在可能比 NSThread 的名字更有用。gdb 和 Instruments 不知道 NSThread 的名称,只知道 pthread 级别名称。
回答by Tom Seddon
One disadvantage of using a Mach port name to identify a thread is that the name returned is local to the calling process. If several tasks retrieve a particular task's threads (using task_threads
), each task will retrieve a different port name for a particular thread.
使用 Mach 端口名称来标识线程的一个缺点是返回的名称对于调用进程来说是本地的。如果多个任务检索特定任务的线程(使用task_threads
),则每个任务将为特定线程检索不同的端口名称。
On OS X, you can retrieve a unique 64-bit identifier using thread_info
. This identifier is global (it is the same for a given thread, no matter which task is querying it) and unique (no other thread will ever have the same ID, now or in the future, until after reboot of course - as a 64-bit value, overflow is unlikely).
在 OS X 上,您可以使用thread_info
. 这个标识符是全局的(对于给定的线程来说是相同的,无论哪个任务正在查询它)并且是唯一的(现在或将来,没有其他线程将拥有相同的 ID,直到重启后当然 - 作为 64 -bit 值,溢出是不可能的)。
(See XNU source, XNU source.)
Retrieve this identifier for a pthread using code along these lines:
使用以下代码行检索 pthread 的此标识符:
uint64_t GetThreadID(pthread_t thread) {
mach_port_name_t port=pthread_mach_thread_np(thread);
thread_identifier_info_data_t info;
mach_msg_type_number_t info_count=THREAD_IDENTIFIER_INFO_COUNT;
kern_return_t kr=thread_info(thread,
THREAD_IDENTIFIER_INFO,
(thread_info_t)&info,
&info_count);
if(kr!=KERN_SUCCESS) {
/* you can get a description of the error by calling
* mach_error_string(kr)
*/
return 0;
} else {
return info.thread_id;
}
}
(See XNU source.)
(见XNU 源。)
Two notes:
两个注意事项:
There's no documentation for
THREAD_IDENTIFIER_INFO
, or at least none that I've been able to find. So I suppose, strictly speaking, that makes it undocumented. But it's in the public headers, right next toTHREAD_BASIC_INFO
, which isdocumented - so I'm assuming this is simply an oversight. It's not like the documentation for any of this stuff is particularly great.)I don't know what the situation is on iOS, but
THREAD_IDENTIFIER_INFO
andpthread_mach_thread_np
both appear to be available in the headers, so it could be worth a try.
没有关于 的文档
THREAD_IDENTIFIER_INFO
,或者至少没有我能够找到的文档。所以我想,严格来说,这使得它没有记录。但它在公共标题中,就在 旁边THREAD_BASIC_INFO
,已记录在案 - 所以我假设这只是一个疏忽。并不是所有这些东西的文档都特别好。)我不知道的情况是iOS上的东西,但
THREAD_IDENTIFIER_INFO
和pthread_mach_thread_np
这两个似乎是在头可用,所以它可能是值得一试。