Objective C 线程中线程变量的生命周期

发布于 2024-11-13 12:02:13 字数 1938 浏览 3 评论 0原文


我有一个 NSOperation,在其 -main 方法中使用 [NSThread detachNewThreadSelector:@selector(aMethod:) toTarget:self withObject:anArgument];

aObject (我的实例变量NSOperation 子类)是对 -main 方法内返回的自动释放数组的对象的弱引用...

-(void)main {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSArray *clients = [group produceClients]; // clients array is an autorelease instance
    self->aObject = [clients objectAtIndex:3]; // a weak reference, Lets say at index three!

    [NSThread detachNewThreadSelector:@selector(aMethod:) 
                             toTarget:self 
                           withObject:@"I use this for another thing"];

    // Do other things here that may take some time

    [pool release];
}

-(void)aMethod:(NSString*)aStr {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // aStr object is reserved for another functionality
    // I know that I could pass a NSDictionary with a number of entries, which would be
    // retained by `detachNewThreadSelector` but then ... 
    // I wouldn't have to ask this question ! :)

    // Do several things with aObject, which is a weak reference as described above.
    NSLog(@"%@", [self->aObject.id]);
    // Is it safe ?

    [pool release];
}

我知道 NSThread 的 detachNewThreadSelector 方法保留 self 和withObject:anArgument,但是 aObject 会发生什么?是否确定在执行分离线程 (aMethod:) 期间会存在? Self 被 detachNewThreadSelector 保留,这是否意味着主线程的池将被延迟释放,因为它被保留,因此 clients 将存在,因此 >aObject 会存在吗?
或者 -main (NSOperation) 线程将在 -aMethod (NSThread) 完成之前完成执行并释放,因此在那里使用 aObject 是不安全的?

真正的问题是:当从线程内部调用 [NSThread detachNewThreadSelector:@selector(aMethod:) ...toTarget:self ...] 时,最后一个线程是否以它的方式保留自动释放的实例(clients 数组)可以安全地在 aMethod (self->aObject) 中使用(可以说通过弱引用)?

I have an NSOperation where inside its -main method I use [NSThread detachNewThreadSelector:@selector(aMethod:) toTarget:self withObject:anArgument];

aObject (instance variable of my NSOperation subclass) is a weak reference to an object of an autoreleased array returned inside the -main method...

-(void)main {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSArray *clients = [group produceClients]; // clients array is an autorelease instance
    self->aObject = [clients objectAtIndex:3]; // a weak reference, Lets say at index three!

    [NSThread detachNewThreadSelector:@selector(aMethod:) 
                             toTarget:self 
                           withObject:@"I use this for another thing"];

    // Do other things here that may take some time

    [pool release];
}

-(void)aMethod:(NSString*)aStr {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // aStr object is reserved for another functionality
    // I know that I could pass a NSDictionary with a number of entries, which would be
    // retained by `detachNewThreadSelector` but then ... 
    // I wouldn't have to ask this question ! :)

    // Do several things with aObject, which is a weak reference as described above.
    NSLog(@"%@", [self->aObject.id]);
    // Is it safe ?

    [pool release];
}

I know that NSThread's detachNewThreadSelector method retains self and withObject:anArgument, but what happens to aObject ?? Is it sure that will exist during the execution of the detached thread (aMethod:) ? Self is retained by detachNewThreadSelector, does this mean that the pool of the -main thread will be delayed released since it is retained and thus the clients will exist and thus the aObject will exist ?
Or the -main (NSOperation) thread will finish execution and released before -aMethod (NSThread) finishes so it's unsafe to use aObject there ?

The real question is: When calling [NSThread detachNewThreadSelector:@selector(aMethod:) ...toTarget:self ...] from inside a thread, does the last thread being retained in a way that its autoreleased instances (clients array) are safe to be used in aMethod (self->aObject) (lets say via weak references) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青朷 2024-11-20 12:02:13

你的方法似乎非常不稳定,但我不是多线程专家,所以我可能是错的。您的客户端数组位于主自动释放池中,您不能保证它会等到您的 aMethod 线程完成耗尽。怎么样:

-(void)main {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [NSThread detachNewThreadSelector:@selector(aMethod:) 
                             toTarget:self 
                           withObject:@"I use this for another thing"];

    [pool release];
}

-(void)aMethod:(NSString*)aStr {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSArray *clients = [group produceClients]; // clients array is an autorelease instance
    self->aObject = [clients objectAtIndex:3]; // a weak reference, Lets say at index three!

    [pool release];
}

通过这种方法,您的客户端数组位于线程的自动释放池中。

Your approach seems highly unstable, but I'm not an expert on multithreading so I could be wrong. Your clients array is in the main autorelease pool, which you cannot be assured will wait till your aMethod thread is completed to drain. What about this:

-(void)main {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [NSThread detachNewThreadSelector:@selector(aMethod:) 
                             toTarget:self 
                           withObject:@"I use this for another thing"];

    [pool release];
}

-(void)aMethod:(NSString*)aStr {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSArray *clients = [group produceClients]; // clients array is an autorelease instance
    self->aObject = [clients objectAtIndex:3]; // a weak reference, Lets say at index three!

    [pool release];
}

With this approach, your clients array is in the thread's autorelease pool.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文