Nsthread Number on iOS

NSThread number on iOS?

That number is actually an ivar in NSThread's private implementation class. The class is _NSThreadInternal, and its name is "_private". Inside that object, the ivar is seqNum.

You can pull it directly if you're willing to rely on undocumented key paths. This'll do it (and good call neilsbot on using valueForKeyPath instead of runtime calls):

@implementation NSThread (GetSequenceNumber)

- (NSInteger)sequenceNumber
{
return [[self valueForKeyPath:@"private.seqNum"] integerValue];
}

@end

I tested it by manually setting that ivar with runtime calls and then NSLogging the thread. Sure enough, the description reflected the change. This is obviously not documented, so...

...use at your own risk.

It's a fun exercise, but things are typically private for a reason. Shipped code should certainly avoid things like this unless all other routes have been thoroughly exhausted.

meaning of the returned value of [NSThread currentThread]

The first is the memory address. The second is the thread name. You can set the name of a thread. The third number can be explained in these posts:

See:

po [NSThread currentThread]

and

NSThread number on iOS?

po [NSThread currentThread]

Thread numbers are meaningless, pretty much.

The thread instance, though, is a singleton per thread. You could use the NSThread's address, by coincidence. Better, still, would be to dip down to the mach_* API and grab the thread ID from that API.

[NSThread currentThread] is about as unique of a number as you'll get. If the thread terminates and then a new thread is created, you might see the same address vended. The mach APIs will vend something just about as unique, really.

What are you trying to do?

How do I stop this NSThread?

Calling cancel on a thread doesn't automatically kill the thread. The actual body of the thread needs to stop whatever it is doing when its thread is cancelled.

Update your task function like this:

@objc static func task() {
while (!NSThread.currentThread.cancelled)
{
sleep(3);
print("we are still running!")
callback?()
}
}

Double check the actual method and property names for currentThread and cancelled. I'm not 100% sure what they are named in Swift 2.

Even with the above, you will likely get one more call to callback after the thread is cancelled due to the sleep. You can fix this with:

@objc static func task() {
while (!NSThread.currentThread.cancelled)
{
sleep(3);
print("we are still running!")
if (!NSThread.currentThread.cancelled) {
callback?()
}
}
}

Getting thread id of current method call

NSLog(@"%@", [NSThread currentThread]);

How to get a parent thread for NSThread in iOS?

You can't. There is no such thing as parent thread. A thread is an independent entity, even if a thread can communicate with other threads but there is no hierarchy involved.

Call a function that will start a NSThread on a pointer to function

selector is already a selector. Don't wrap it with @selector.

- (void)callInBackground:(id)target selector:(SEL)selector
{
NSThread* thread = [[NSThread alloc] initWithTarget:self selector:selector object:nil];
[thread setStackSize: 2*1024*1024];
[thread start];

while([thread isFinished] == NO) { usleep(1000); }
[thread release];
}

Note, NSThread is hard to get working correctly. I don't think the above code will do what you want.

For a better answer, look into Dispatch. Dispatch is also called GCD or Grand Central Dispatch.



Related Topics



Leave a reply



Submit