什么时候发布NSThread是安全的? [英] when is it safe to release an NSThread?

查看:100
本文介绍了什么时候发布NSThread是安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的辅助NSThread * processThread的运行循环

Below is the runloop for my secondary NSThread* processThread

关闭我调用的主题

//cancel secondary thread
[processThread cancel]
//signal condition
[processCondition broadcast];

然后可以安全地致电:

[processCondition release];
[processThread release];

还是我需要确定线程已经完成?




或许这样?

or do i need to be sure that the thread has finished?

Perhaps like this?

NSTimeInterval timeout = [NSDate timeIntervalSinceReferenceDate] + (1.0/15.0);

while ([processThread isExecuting] && [NSDate timeIntervalSinceReferenceDate] < timeout)
{
    [NSThread sleepForTimeInterval: 1.0/1000.0 ];
}

[processCondition release];
[processThread release];







详细代码和解释:




detailed code and explanation:

- (void)processLoop
{
    NSAutoreleasePool * outerPool = [[NSAutoreleasePool alloc] init];
    [processCondition lock];

    //outer loop    
    //this loop runs until my application exits
    while (![[NSThread currentThread] isCancelled])    
    {
        NSAutoreleasePool *middlePool = [[NSAutoreleasePool alloc];
        if(processGo)
        {
            //inner loop
            //this loop runs typically for a few seconds
            while (processGo && ![[NSThread currentThread] isCancelled]) 
            {
                NSAutoreleasePool *innerPool = [[NSAutoreleasePool alloc]; init];
                //within inner loop
                //this takes a fraction of a second
                [self doSomething];
                [innerPool release];
            }
            [self tidyThingsUp];

        }
        else
        {
            [processCondition wait];
        } 
        [middlePool release];
    }
    [processCondition unlock];      
    [outerPool release];
}

组合:


  • 内部循环

  • NSCondition * processCondition

  • 切换 processGo 介于 YES

  • an inner while loop
  • NSCondition *processCondition
  • toggling processGo between YES and NO

允许我在不取消线程的情况下停止并启动内部while循环。

allows me to stop and start the inner while loop without cancelling the thread.

if (processGo == YES)



执行进入内部while循环。


execution enters the inner while loop.

当主线程设置

processGo = NO

执行离开内部while循环并在外循环的下一次传递中整理
,执行命中

execution leaves the inner while loop and tidys up
on the next pass of the outer loop, execution hits

[processCondition wait]

等待

如果主线程重置

processGo == YES

和来电

[processCondition wait]

执行重新进入内部循环

execution re-enters the inner loop

推荐答案

是的,如果你已经完成了对NSThread的调用,可以安全地调用它。在非GC Objective C代码中,习惯用法是,一旦您完成访问对象,您就可以释放它。如果还有其他东西需要该对象,包括对象本身,那么他们的工作就是保留它。一般情况下,如果一个物体无法在任意时间安全地处置,它会在处于不安全状态时自行保留,并在可以安全处置时自行释放。

Yes, it is safe to call release against an NSThread if you are done with it. In non-GC Objective C code the idiom is that once you are done accessing an object you may release it. If anything else needs that object, including the object itself it their job to have a retain against it. In general if an object cannot be safely disposed at arbitrary times it will retain itself while it is in an unsafe state, and release itself when it can be safely disposed of.

这就是NSThread和NSURLConnection之类的工作方式(NSURLConnection实际上保留了它的委托,并且做了很多花哨的事情来应对发生的保留循环。

This is how things like NSThread and NSURLConnection work (NSURLConnection actually retains its delegate and does a lot of fancy stuff to cope with the retain loop that occurs.

这篇关于什么时候发布NSThread是安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆