[devdiv精华转载] ios开发 如何结束一个NSThread线程
如何结束一个NSThread线程
本人在主线程中通过
NSThread *newThread = [[NSThread initWithTarget:self selector:@selector(MyThread:) object:timer]; 开始一个新线程
当满足条件时欲调用[newThread cancel]退出此线程,但发现线程并未退出,MyThread函数仍然在执行
该如何结束此线程
可以用isMultiThreaded和exit来解决这个问题。cancel还不是terminate
exit
Terminates the current thread.
+ (void)exit
Discussion
This method uses the currentThread class method to access the current thread. Before exiting the thread, this method posts the NSThreadWillExitNotification with the thread being exited to the default notification center. Because notifications are delivered synchronously, all observers of NSThreadWillExitNotification are guaranteed to receive the notification before the thread exits.
Invoking this method should be avoided as it does not give your thread a chance to clean up any resources it allocated during its execution.
注意的是调用这个之前要把你线程里面申请的内存释放掉。
浙公网安备 33010602011771号