• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Main thread to another
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Main thread to another


  • Subject: Re: Main thread to another
  • From: Kurt Revis <email@hidden>
  • Date: Mon, 10 Dec 2001 02:54:57 -0800

On Monday, December 10, 2001, at 01:59 AM, Dustin Mierau wrote:

I am storing the value of [NSThread currentThread] in an NSArray which I am passing to a new NSThread. Problem is, when I enter in the new thread, the current thread now changes, obviously, and so does this retained NSThread I stored NSArray.

This isn't really the case. Try checking the pointer values of the two threads--you'll see that they're different. Or [thread1 isEqual:thread2] -- that should return NO.

What IS the case is that different threads seem to be returning the same description. So if you NSLog(@"%@", [NSThread currentThread]), you'll see misleading results. I'm going to report this one as a bug.

For what it's worth, here's my test code (copy this into a new Foundation Tool project) and the results.

----------------------------------------------------

#import <Foundation/Foundation.h>

static NSThread *mainThread = nil;

@interface MyObject : NSObject { }

- (void)doThread:(id)object;

@end

@implementation MyObject

- (void)doThread:(id)object
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSThread *currentThread = [NSThread currentThread];

NSLog(@"2: Current thread: %p %@", currentThread, currentThread);
NSLog(@"3: Main thread: %p %@", mainThread, mainThread);
if (mainThread == currentThread)
NSLog(@"The two threads' pointers are equal, which is weird.");
else
NSLog(@"The two threads' pointers are different, which is expected.");

if ([mainThread isEqual:currentThread])
NSLog(@"[mainThread isEqual:currentThread] returns YES, which is weird.");
else
NSLog(@"[mainThread isEqual:currentThread] returns NO, which is expected.");

if ([[mainThread description] isEqual:[currentThread description]])
NSLog(@"The two threads have the same description, though, which sure is annoying.");
else
NSLog(@"The two threads have different descriptions, as you would expect.");

[pool release];
}

@end

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
MyObject *myObject;

mainThread = [[NSThread currentThread] retain];
NSLog(@"1: Main thread: %p %@", mainThread, mainThread);

myObject = [[MyObject alloc] init];

[NSThread detachNewThreadSelector:@selector(doThread:) toTarget:myObject withObject:nil];

while (1); // Let the other thread work

[pool release];
return 0;
}

----------------------------------------------------

2001-12-10 02:52:40.965 Threadz[1527] 1: Main thread: 0x59340 Thread 0x800013b8
2001-12-10 02:52:41.029 Threadz[1527] 2: Current thread: 0x60f30 Thread 0x60cd0
2001-12-10 02:52:41.030 Threadz[1527] 3: Main thread: 0x59340 Thread 0x60cd0
2001-12-10 02:52:41.030 Threadz[1527] The two threads' pointers are different, which is expected.
2001-12-10 02:52:41.030 Threadz[1527] [mainThread isEqual:currentThread] returns NO, which is expected.
2001-12-10 02:52:41.030 Threadz[1527] The two threads have the same description, though, which sure is annoying.


  • Follow-Ups:
    • Re: Main thread to another
      • From: Dustin Mierau <email@hidden>
References: 
 >Main thread to another (From: Dustin Mierau <email@hidden>)

  • Prev by Date: Re: logout and auto-login
  • Next by Date: Re: Main thread to another
  • Previous by thread: Main thread to another
  • Next by thread: Re: Main thread to another
  • Index(es):
    • Date
    • Thread