[英]What is the best way to signal a loop to exit on a thread in Cocoa?
我有一個方法(我們稱之為運行):
- (void)run
{
// Do some initialisation
// Loop until another thread signals it to exit
while (SHOULD_STILL_LOOP) { ... }
// Clean up code
}
我這樣稱呼:
[self performSelectorInBackground:@selector(run)];
實施SHOULD_STILL_LOOP的最佳方法是什么? 我應該使用原子屬性NSCondition調度信號量嗎?
也許有些堆可以給我一些建議?
謝謝。
我找到了一種方法,那就是使用NSThread。
- (void)run:(id)obj
{
while(![[NSThread currentThread] isCancelled]) { ... }
}
- (void)start
{
self.thread = [[NSThread alloc] initWithTarget:self
selector:@selector(run)
object:self.object];
[self.thread start];
}
- (void)stop
{
[self.thread cancel];
self.thread = nil;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.