[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: thread question
From: |
Richard Frith-Macdonald |
Subject: |
Re: thread question |
Date: |
Thu, 27 Dec 2001 18:22:15 +0000 |
On Thursday, December 27, 2001, at 06:03 PM, Aurelien wrote:
Well, first I must admit that parts of my question come from a java
miscomprehension, things I haven't yet fully understood from my long
experience with this language.
I am porting a project from java to objc and in the java project
there's an area that makes extensive uses of threads, and of course,
takes good care of synchronizing them.
generally, I know how to handle code like this:
synchronized void doSomething () {
// do something
}
-> I translate that into:
- (void) doSomething
{
NS(Recursive)Lock *aLock;
aLock = [[NS(Recursive)Lock alloc] init];
[aLock lock];
// do something
[aLock unlock];
}
so far, so good.
No .... the lock needs to be created beforehand. You could have a single
global lock
created at the start of your program, and do something like this.
- (void) doSomething
{
static NS(Recursive)Lock *aLock = nil;
if (aLock == nil)
{
[globalLock lock];
if (aLock == nil)
{
aLock = [[NS(Recursive)Lock alloc] init];
}
[globalLock unlock];
}
[aLock lock];
// do something
[aLock unlock];
}
Now, what I don't understand is the use in java of the wait(),
notify () and notifyAll() methods.
There is really no direct equivalent ... but NSConditionLock should
cover most of this I think.