首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我使用的NSLock不起作用?

为什么我使用的NSLock不起作用?
EN

Stack Overflow用户
提问于 2011-12-27 17:50:34
回答 1查看 2.1K关注 0票数 3

我正在编写代码来渲染和旋转图片,其详细信息正在同时计算和更新。它在单线程上工作时没有错误(有一个显示链接),但看起来很笨拙,我不想让显示链接触发计算。因此,我希望在主线程中执行所有与OpenGL相关的代码(使用display链接),并在第二个线程中执行所有计算(执行while (YES)循环)。

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

然后,我在模型对象中定义了一个NSLock,并将其锁定为所有写入(在模型类中)和读取(在视图类中)……但它仍然会导致相同的错误,并且图形仍然偶尔会出现奇怪的闪烁。

是我做错了什么,还是我的问题出在别的地方?

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

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

代码语言:javascript
复制
@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放在我的模型类中:

代码语言:javascript
复制
@interface MyModel : NSObject {
    NSLock* oLock;
    ...
}
@property (nonatomic, strong) NSLock* oLock;

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

在我的视图类中:

代码语言:javascript
复制
@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];
}
...

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2012-01-15 02:25:57

我认为在这种情况下使用中央调度会容易得多。

代码语言:javascript
复制
@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];
    });
}

模型类

代码语言:javascript
复制
@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, ^{
        ...
    });
}
...

视图

代码语言:javascript
复制
@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的使用量。

代码语言:javascript
复制
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开销。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8643229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档