discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

thread question


From: Aurelien
Subject: thread question
Date: Thu, 27 Dec 2001 19:03:02 +0100

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.

Now, what I don't understand is the use in java of the wait(), notify () and notifyAll() methods.

This is what the java documentation says about the "wait" method:

-------------BEGIN CITATION-----------------

Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other word's this method behaves exactly as if it simply performs the call wait(0).

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.

-------------END CITATION-----------------

First, I'm not sure what they mean by "monitor". Is it a lock ? But then, why do they associate it with an Object: in objc, an NSLock is never associated with an Object, but with a particular section of code. So ok, let's say that what it means is that the thread must be in a synchronized block. Hence, saying "The thread releases ownership of this monitor" is like doing [aLock unlock]. Then what ? Another thread could access the particular block of code and that's exactly what we want to avoid. Then there's this "notification" story. Do we have something similar in objective-c.

Having never used this mechanism because I never understood these explanations, my intuition tells me that wait() and notify() could mean something like:

wait (): all Threads now, please don't you access this code, it's not useable, so be patient !
notify: ok, threads, this code's now ready, please do your stuff

Only, I don't see an obvious way to emulate this in objc.

But I'm sure this has certainly be addressed a 1000 times.

Aurélien




reply via email to

[Prev in Thread] Current Thread [Next in Thread]