我正在编写代码来渲染和旋转图片,其详细信息正在同时计算和更新。它在单个线程(带有显示链接)上无错误地工作,但看起来很笨拙,而且我不希望由显示链接触发计算。所以我想在主线程中执行所有与opengl相关的代码(带有显示链接),并在第二个线程中执行所有计算(执行while (YES)循环)。
我使用NSThread实现了这个。它在一段时间内工作得很好,然后在glDrawArrays期间出现'Thread 1: Program received signal: "EXC_BAD_ACCESS"'失败,有时会出现奇怪的图形闪烁。这是我所期望的,如果主线程读取模型级数据的同时,第二个线程正在覆盖它。
然后我在模型对象中定义了一个NSLock,并锁定了它的所有写入(在我的模型类)和读取(在我的视图类)…但它仍然会导致相同的错误,并且图形偶尔也会出现奇怪的闪烁。
是我在这里做错了什么,还是我的问题在别处?
其次,在这种情况下,停止第二个线程的正确方法是什么?NSThread类引用建议使用cancel,检查isCancelled,如果是的话退出,但它也说应该避免调用exit。
这里是修改代码-在我的控制器类(我使用XCode 4.2与ARC;我所有的变量都是非原子的):
@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];
}
...
在我的视图类中:
@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];
}
...
谢谢!
我认为在这种情况下使用全局调度会容易得多。
@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);