Creating NSLock ?
Creating NSLock ?
- Subject: Creating NSLock ?
- From: email@hidden
- Date: Wed, 11 Jul 2001 22:02:44 +0200
Who can explain to me how to create new NSLocks in a multithreaded app?
Apple's doc says:
It's safest to create all of the locks before the application becomes
multi-threaded, to avoid race conditions. If you want to create
additional locks after the application becomes multi-threaded, you should
create the new lock inside a critical code section that is itself
protected by an existing lock.
So I do the following when I need a new lock:
NSLock *CREATE_LOCK; // inited at startup
@implementation ....
+(NSLock*) newGraphicsPortLock {
NSLock *lock;
[CREATE_LOCK lock];
lock = [[NSLock alloc] init];
[CREATE_LOCK unlock];
[lock autorelease];
return lock;
}
This seems to work... but only once. If the user interrupt the computation,
my working thread receives [NSThread exit] AND STAY there (it is not
really deallocated).
The problem is that, when a new computation starts (with a new freshly
created lock), THE WHOLE APP gets blocked when the working thread tries to
acquire the lock the first time. Through the debugger, I can see that ONLY
the working thread is trying to acquire the lock: the main thread is
inside the standard run loop in [NSApplication nextEventMatchingMask:],
apparently blocking on mach_msg_overwrite_trap (and it never progress, so
the app is blocked); other threads are on semaphore_wait_signal_trap, and
the working thread deadlocks alone... so I suspect that the lock itself is
incorrect?
Thomas Lachand-Robert
********************** email@hidden
The Commandant Project:
http://lachand.free.fr/
<< Et le chemin est long du projet ` la chose. >> Molihre, Tartuffe.