Objective C 线程中线程变量的生命周期
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的方法似乎非常不稳定,但我不是多线程专家,所以我可能是错的。您的客户端数组位于主自动释放池中,您不能保证它会等到您的 aMethod 线程完成耗尽。怎么样:
通过这种方法,您的客户端数组位于线程的自动释放池中。
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:
With this approach, your clients array is in the thread's autorelease pool.