Re: Question about Threading
Re: Question about Threading
- Subject: Re: Question about Threading
- From: James Bucanek <email@hidden>
- Date: Wed, 10 Aug 2005 12:07:18 -0700
Chris Kane wrote on Wednesday, August 10, 2005:
>I'm not saying the above example does this, just providing the
>following warning...
>
>Don't, however, try to solve the synchronization and "wait until the
>background thread is done" by locking a lock in one thread which the
>other thread unlocks. That invokes undefined behavior in the
>underlying POSIX lock objects, and is a bad idea. I warn about this
>because it is tempting to do because it seems simple. One should
>never use mutex locks to implement "conditional waiting" is the
>point; that is, never use -lock on a mutex in a "block until
>something happens" sense, where the event is signalled by some other
>thread (obviously, since the current thread is now blocked on the
>lock) unlocking the previously locked lock.
Whoa, Chris. You're completely freaking me out! If I understand what you wrote correctly, you're saying that if thread A gets a lock on an NSLock, then thread B shouldn't release it? I've been doing exactly this for donkey's years:
- Create and lock some result object
- Start a new thread with the calculation of that object
- New thread starts and will release the lock on the result object when done
- Go do other stuff
- When I want the results, I request a second lock on the result object.
- When I obtain the lock, I read the results.
I don't know any other way of accomplishing this. I can't have the new thread create the initial lock on the result object, because as soon as I start the thread my two threads are in a race condition to make the first lock.
What other mechanism are you supposed to use to accomplish this? I'm looking at NSConditionalLock, but it's not immediately obvious to me how this solves the problem. I'm guessing that you're advocating some kind of state machine where a double-handshake is used to get the two threads sync'd up beyond the initial race condition when the second thread is created but hasn't obtained any locks yet. Is that the general idea, or am I completely mis-reading all of this?
Does anyone have some example code?
--
James Bucanek
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden