Here's pseudo code of what I have in a method:
NSCondition condition = [[NSCondition alloc] init];
int predicate = 0;
dispatch_sync(dispatch_get_main_queue(), ^
{
[condition lock]; // Lock-0
});
bindBlock1ForDataReceived(^()
{
// Not main thread here.
// Get on main thread, because lock and unlock must be run on same thread.
dispatch_sync(dispatch_get_main_queue(), ^
{
predicate = 1;
[condition signal];
[condition unlock]; <<<<---- "unlocked when not locked"
});
});
bindBlock2ForNoDataAvailable(^()
{
// Not main thread here.
// Get on main thread, because lock and unlock must be run on same thread.
dispatch_sync(dispatch_get_main_queue(), ^
{
predicate = 2;
[condition signal];
[condition unlock];
});
});
[condition lock]; // Lock-1
while (predicate == 0)
{
[condition waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:5.0]];
}
[condition unlock];
if (predicate == 2)
{
[condition lock]; // Lock-2
[condition waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]];
[condition unlock];
}
The issue is that I get an "unlocked when not locked" warning from iOS (see above) when first event 2 occurs and then event 1.
Now let me explain what I'm trying to accomplish: This is part of a data fetcher. Normal cases data is received and block1 is executed: no issues. Sometimes the no-data block2 is spuriously executed first, shortly followed by block1; this is when I get the NSCondition
warning. To catch this rare case, I wait for 2.0
seconds. Here's what happens:
- Block2 signals the condition.
- Lock-1 falls through.
predicate
is no longer0
so there's no wait.- The condition is unlocked again.
- We then continue to the
if
-statement whose condition(predicate == 2)
is true. - The method gets Lock-2 immediately. <<<< ROOT CAUSE
- Subsequently the method wait for
2.0
seconds. - Within these 2 seconds block1 is executed and signals the condition.
- Then block1 unlocks the condition & the method unlocks as well.
The root cause (see above) is that the lock is acquired by the method (the worker/consumer), while it should have been acquired by the data producer. I've spend a lot of time trying to figure this out; one of the thoughts I had is using two NSCondition
s, but I could not figure this out because things are rather intertwined.
Note: I find it strange that the warning does not appear at the unlock
inside the if
-statement.
Thanks for your time!
The simplest approach with more than lock is to use