objective-c - 为什么我对 NSLock 的使用不起作用?

标签 objective-c ios multithreading opengl-es nsthread

我正在编写代码来渲染和旋转一张图片,其细节正在同时计算和更新。它在单个线程(带有显示链接)上无错误地工作,但看起来很笨重,而且我不希望显示链接触发计算。所以我想在主线程(带有显示链接)中执行所有与 OpenGL 相关的代码,并在第二个线程中执行所有计算(执行 while (YES) 循环)。

我使用 NSThread 实现了这个。它可以很好地工作一段时间,然后在 glDrawArrays 期间因“线程 1:程序接收到信号:“EXC_BAD_ACCESS””而失败,并且有时会出现奇怪的图形闪烁。如果主线程在第二个线程覆盖它的同时读取模型级数据,这就是我所期望的。

然后我在模型对象中定义了一个 NSLock 并锁定它以供所有写入(在我的模型类中)和读取(在我的 View 类中)......但它仍然会导致相同的错误,并且图形仍然偶尔有奇怪的闪光。

是我这里做错了什么,还是我的问题在其他地方?

其次,在这种情况下停止第二个线程的正确方法是什么? NSThread 类引用建议使用 cancel,检查 isCancelled,如果是则退出,但它也说应该避免调用 exit。

以下是对代码的修改 - 在我的 Controller 类中(我使用带有 ARC 的 XCode 4.2;我所有的 ivar 都是非原子的):

@interface MyController : NSObject {
    NSThread *calcThread;
    ...
}
// (I do not give it an @property or @synthesize line)

@implementation MyController
- (void) awakeFromNib {
    calcThread = [[NSThread alloc] initWithTarget:self 
            selector:@selector(calcLoop:) object:nil];
    [calcThread start];
    ...
}
- (void) calcLoop:(id)arg { 
    @autoreleasepool {
        while (YES)
            [myModel calculate];
    }
}
...

我把 NSLock 放在我的模型类中:

@interface MyModel : NSObject {
    NSLock* oLock;
    ...
}
@property (nonatomic, strong) NSLock* oLock;

@implementation myModel
-(id) init {
    oLock = [[NSLock alloc] init];
    ...
}
-(void) changeModelAppearance {
    [oLock lock];
    ...
    [oLock unlock];
}
...

在我的 View 类中:

@implementation MyView
-(void) modelUpdated:(NSNotification *) notification {
// modelUpdated is called via the NSNotificationCenter
    MyModel* myModel = (MyModel*) [notification object];
    [myModel.oLock lock];
    ... // update OpenGL structures with data from myModel
    [myModel.oLock unlock];
}
...

谢谢!

最佳答案

我认为在这种情况下使用 grand central dispatch 会容易得多。

@interface MyController : NSObject { // Not use why you're not inheriting from NSController here.
    dispatch_queue_t calQueue;
    ...
}

- (void) awakeFromNib {
    calcQueue = dispatch_queue_create("com.yourApp.calc", DISPATCH_QUEUE_SERIAL);
    dispatch_async(calcQueue, ^{
        while(YES) // This will peg the CPU at 100%
            [myModel calculate];
    });
}

模型类

@interface MyModel : NSObject {
    dispatch_queue_t modelQueue;
    ...
}
@property dispatch_queue_t modelQueue;

@implementation myModel
-(id) init {
    modelQueue = dispatch_queue_create("com.yourApp.model", DISPATCH_QUEUE_SERIAL);
}

-(void) dealloc {
    dispatch_release(modelQueue);
}

-(void) changeModelAppearance {
    dispatch_async(modelQueue, ^{
        ...
    });
}
...

查看

@implementation MyView
-(void) modelUpdated:(NSNotification *) notification {
// modelUpdated is called via the NSNotificationCenter
    MyModel* myModel = (MyModel*) [notification object];
    dispatch_async(model.modelQueue, ^{
        ... // update OpenGL structures with data from myModel 
    });
}
...

要暂停您只需调用 dispatch_suspend 的任何队列并重新启动任何队列,请使用 dispatch_resume

如果您使用计时器而不是无限循环,您可以减少所使用的 CPU 数量。

calcTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
dispatch_source_set_timer(calcTimer, DISPATCH_TIME_NOW, DT, 1000);
dispatch_source_set_event_handler(calcTimer, ^{
    ...
});
dispatch_resume(calcTimer);

这将使用更低的 CPU 开销。

关于objective-c - 为什么我对 NSLock 的使用不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8643229/

相关文章:

ios - 长按多次调用 UIButton 的操作

ios - QMChatViewController 导致 "Cannot assign to property: ' self' 是不可变的

java - 运行 HttpServer 时无法创建新的 native 线程

objective-c - 避免 objmsg 函数开销?

iphone - UINavigationController + UITabBar。 viewDidLoad 中的垂直定位错误

ios - NSString正则表达式查找和替换

c - malloc 本身是否提供某种同步?

objective-c - 无法将表达式的类型 'Void' 转换为类型 'String!'

ios - 主从 UISplitviewcontroller 添加为选项卡栏项目之一 - Show Master

java - 如何使用 XMLEncoder 使 Arraylist 线程安全且可序列化