How to determine whether to wait for a send semaphore?

How can you determine if it is waiting dispatch_semaphore_ton w / out, causing a wait? I originally thought:

if ( dispatch_semaphore_wait(mySemaphore, DISPATCH_TIME_NOW) ) {
    NSLog(@"No more resources, wait");
} else {
    NSLog(@"Resources available, shouldn't wait");
}

But the do dispatch_semaphore_wait()semaphore's effect decreases, so I thought:

if ( dispatch_semaphore_wait(mySemaphore, DISPATCH_TIME_NOW) ) {
    NSLog(@"No more resources, wait");
} else {
    dispatch_semaphore_signal(mySemaphore);
    NSLog(@"Resources available, shouldn't wait");
}

Who has the end result, not reducing the semaphore, but it seems like hacking, sentences?

EDIT

How I typed what I was doing, and how I came to this conclusion, I realized that I was really thinking about the right decision, I just wanted to know if I was waiting on the resource and if I could show the user "wait". I think this is the right way to do this:

    [self showWait];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        dispatch_semaphore_wait(mySemaphore, DISPATCH_TIME_FOREVER);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self hideWait];
        });
    });
+5
source share
1 answer

dispatch_semaphore_wait() only reduced the value of the semaphore if it returns 0.

- (.. ), .

, , , , , , , , , .

?

+4

All Articles