Re: NSLock locking order;
Re: NSLock locking order;
- Subject: Re: NSLock locking order;
- From: Joseph Kelly <email@hidden>
- Date: Thu, 10 Nov 2005 12:15:11 -0800
NSLock is based on pthreads, whose docs often mention the fact that
there are no guarantees as to which thread will acquire a lock. If
you need to synchronize your threads, then NSConditionLock is your
new friend:
Main Thread:
[_oLock initWithCondition:THREAD_A_WORK];
...
Thread A:
[_oLock lockWhenCondition:THREAD_A_WORK];
...
[_oLock unlockWithCondition:THREAD_B_WORK];
Thread B:
[_oLock lockWhenCondition:THREAD_B_WORK];
...
[_oLock unlockWithCondition:THREAD_C_WORK];
Thread C:
[_oLock lockWhenCondition:THREAD_C_WORK];
...
[_oLock unlockWithCondition:THREAD_A_WORK];
On Nov 10, 2005, at 10:37 AM, Matt Budd (Madentec) wrote:
Hello all,
Say I have three threads competing for the same NSLock. Here's some
small psuedo code:
Thread A, B, and C (just with different "<stuff>")
--------------------------------------------------------------
while (YES) {
[_oLock lock];
<stuff>
[_oLock unlock];
[NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow:
0.5]];
}
So lets say thread A got the the lock first, and then then got
swapped out while doing "<stuff>". Then thread B blocks attempting
to acquire the lock. Then thread C runs and also blocks attempting
to acquire the lock. Then we get back to thread A and it finishes
its "<stuff>", unlocks the _oLock, and sleeps for half a second.
Is there any order to who will get to acquire the lock next? Does
thread B get it because it is queued up somewhere in the
foundation's NSLock class that it is first? Or is there a
possibility that thread A might finish its sleep and loop around
and obtain the lock again? Or does thread C have a chance?
Thanks for any info...
- Matt
_______________________________________________
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
_______________________________________________
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