gpt4 book ai didi

iOS - Objective-C - 如何在等待时停止 NSThread?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:25:50 26 4
gpt4 key购买 nike

我有一个 nsthread,其中有一个 while 循环。它在 main 方法中从“线程安全”队列中获取对象。当我离开包含这个 nsthread 对象的 UIViewController 时,我调用 nsthread cancel 方法,但它并没有停止,因为它被“queueLock”NSCondition 锁定了。当我回到这个 UIViewController 时,将创建一个新的 nsthread 并从队列中获取对象,但是前一个线程仍然存在并且它们都尝试使用队列中的相同对象,这会导致内存管理问题。我的问题是当我离开 UIViewController 时,我应该如何停止这个线程。

NSThread主要方法:

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
while ([self isCancelled] == NO) {
RenderRequest *renderRequest = [queue get];
[self doRender:renderRequest];
[renderRequest release];
}

[pool drain];

这是队列类的get方法:

- (id) get {
id toRet = nil;
[queueLock lock];
@try {
while ([queueContents count] == 0) {
[queueLock wait];
}

toRet = [queueContents lastObject];
[queueContents removeLastObject];
}

@finally {
[queueLock unlock];
return toRet;
}
}

谢谢!

最佳答案

我写了一个简单的demo,希望对你有帮助:)

演示.h

#import <Foundation/Foundation.h>

@interface test : NSObject
{
NSCondition *queueCondition;
NSThread *queueThread;

NSMutableArray *queueTask;

NSTimer *timer;
}
- (id)init;
@end

演示.m

#import "demo.h"

@interface demo (PrivateMethods)
- (void)threadTest;
- (void)cancelThread;
- (void)addTask;
@end


@implementation demo

- (id)init
{
self = [super init];
if (self) {
if (!queueThread) {
if (!queueCondition) {
queueCondition = [[NSCondition alloc] init];
}

if (!queueTask) {
queueTask = [[NSMutableArray alloc] initWithCapacity:5];
}

queueThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadTest) object:nil];
[queueThread start];

[self performSelector:@selector(cancelThread) withObject:nil afterDelay:10];

if (!timer) {
timer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(addTask) userInfo:nil repeats:YES] retain];
}
}
}
return self;
}

- (void)dealloc
{
[queueThread release];
[queueCondition release];
[queueTask release];
[timer invalidate];
[timer release];
[super dealloc];
}

- (void)threadTest
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
while (![[NSThread currentThread] isCancelled]) {
[queueCondition lock];
[queueCondition wait];

if ([queueTask count] == 0) {
[queueCondition unlock];
continue;
}

NSString *str = nil;
while ((str = [queueTask lastObject])) {
NSLog(@"getTask: %@", [queueTask lastObject]);
[queueTask removeLastObject];

}

[queueCondition unlock];
}
NSLog(@"threadTest end");
[pool drain];
}

- (void)addTask
{
[queueCondition lock];
if (!queueTask) {
queueTask = [[NSMutableArray alloc] initWithCapacity:5];
}
[queueTask addObject:@"new task"];
[queueCondition signal];
NSLog(@"add: new task");
[queueCondition unlock];
}

- (void)cancelThread
{
[timer invalidate];

[queueThread cancel];

[queueCondition lock];
[queueCondition signal];
[queueCondition unlock];
}
@end

关于iOS - Objective-C - 如何在等待时停止 NSThread?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9916053/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com