CocoaDev

Edit AllPages

Can two threads lock focus on the same view? Or does one thread block while the other thread completes its drawing?

The answer is: lockFocus makes no attempt to synchronize drawing between threads. Both threads will issue their drawing commands at the same time. This can have disastrous effects when drawing into NSOpenGLViews. The limitations on doing this kind of thing on normal views is less defined – but it’s probably not a good idea.

Here’s a sample view that updates itself on two threads. Simply:

Best of luck!

– MikeTrent

#import <Cocoa/Cocoa.h>

@interface CustomView : NSView { BOOL _forkedThread; NSTimer *_fgTimer; NSTimer *_bgTimer; BOOL _drawGreen;

int _privateData;
NSLock *_drawLock; } @end

@implementation CustomView

// prep work for running a selector on another thread

// manually draw on foreground (normally one would use setNeedsDisplay: for this)

// manually draw on background

@end