pthread -> NSThread

Hi, Given pthread_id (†), is there a way to find the associated NSThread (when the one exists)? Perhaps using an undocumented / unsupported method – I don't mind. Thank you!

(†) the pthread_id is neither of the current nor of the main thread.

Accepted Reply

Given pthread_id …, is there a way to find the associated NSThread (when the one exists)?

No.

Perhaps using an undocumented / unsupported method – I don't mind.

Yeah, but I do (-: DevForums isn’t the right place to discuss unsupported techniques.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Replies

Given pthread_id …, is there a way to find the associated NSThread (when the one exists)?

No.

Perhaps using an undocumented / unsupported method – I don't mind.

Yeah, but I do (-: DevForums isn’t the right place to discuss unsupported techniques.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I was thinking of setting up a signal handler that does Thread.current and assigns the result somewhere (e.g. to a global variable). And then pthread_kill(...) to invoke that signal handler on the thread in question. Do you think this won't work? All the mentioned API's included signal and pthread_kill seem to be supported.

I was thinking of setting up a signal handler that does Thread.current

This is not safe. A signal handler can only call async signal safe functions, and Thread.current isn’t one of those. It’s actually worse than that: Neither the Swift nor Objective-C runtimes are async signal safe. I go into this is more detail in Implementing Your Own Crash Reporter.

What’s your high-level goal here?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

This is not safe

I see, thank you.

Basically I wanted to have the (short) thread numbers compatible with those shown by Xcode's CPU panel. I can get the thread number from NSThread already, but not from a pthread_t.

I can get the thread number from NSThread already

Hmmm, I can’t see any API to do that.

IMO the 64-bit tid is the right way to go here. It’s actually kinda annoying that Xcode uses these short numbers, which are hard to correlate between tools )-:

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"