0

I'm using the open source software TMCache. It saves expensive data to cache asynchronously. There is also a synchronous method.

It uses dispatch_semaphore_wait() to wait until the operation is over.


Source

- (id)objectForKey:(NSString *)key
{
    if (!key)
        return nil;

    __block id objectForKey = nil;

    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

    [self objectForKey:key block:^(TMCache *cache, NSString *key, id object) {
        objectForKey = object;
        dispatch_semaphore_signal(semaphore);
    }];

    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

    #if !OS_OBJECT_USE_OBJC
    dispatch_release(semaphore);
    #endif

    return objectForKey;
}

This works fine on my machine. On a colleague's machine it does not. The program stops working at dispatch_semaphore_wait(). It's absolutely not reproducible for me.

The method above is called in tableView:viewForTableColumn:row:,
so it's executed in the main queue.

Any idea why this is happening? Must I use the method in another queue?

5
  • Maybe the problem is on objectForKey:block. Are you sure that the block is being executed on your colleague's machine?
    – Marcelo
    Oct 10, 2013 at 23:31
  • @MarceloFabri I'm sure it's not, if it was, the semaphore would be signalled and the queue would be unlocked. But why is it not executed...
    – IluTov
    Oct 10, 2013 at 23:39
  • Maybe the key is nil?
    – Marcelo
    Oct 10, 2013 at 23:58
  • @MarceloFabri If the key was nil, dispatch_semaphore_wait() wouldn't be called in the first place...
    – IluTov
    Oct 11, 2013 at 1:01
  • @MarceloFabri Not your fault, I should've posted the whole method for clarity.
    – IluTov
    Oct 11, 2013 at 10:01

1 Answer 1

6

Most likely you are running out of threads. The dispatch_semaphore_signal(semaphore) , which should release the dispatch_semaphore_wait() needs to be executed in a new thread (see objectForKey:block: for details). If OS fails to dispatch that new thread, you stuck, as nobody will send you the dispatch_semaphore_signal.

How often and when it happens depends on computer/device speed, how fast you scroll the table etc. That's why you can't reproduce this issue on your computer.

The quick solution here is to keep number of threads low, by employing the same dispatch semaphore approach with timeout set to DISPATCH_TIME_NOW, as you may not block the main queue.

I would prefer changing the way TMCache.m works, though. I believe that dispatch semaphores approach is not justified in this case - gaining the code brevity (wrapping async method into a synchronous counterpart) at the expense of reliability does not seem right to me. I used to wrap synchronous methods with async ones, but not vice versa.

Here is the fix

https://github.com/rushproject/TMCache

Note that only synchronous objectForKey methods were patched.

4
  • Thank you Alex! On my machine it works perfectly, let me see if it works for my colleague.
    – IluTov
    Oct 12, 2013 at 10:22
  • I have pushed a handful of new commits to refactor async objectForKey:block: - all 3 wrap a call to synchronous counterparts in a dispatched thread. Also some speed optimization by dropping unnecessary calls and postponing object instantiations. Works nice in my app now - I have a huge photo grid filled by photos downloaded form remote server. It scrolls very smoothly now and no deadlocks. Profiler shows about 20 active threads, down from ~60. Oct 12, 2013 at 14:33
  • Thanks! I'm still waiting on feedback so.
    – IluTov
    Oct 12, 2013 at 15:01
  • Dear Alex, sorry to bug you but you may just know the answer to this puzzler, as a semaphore expert! :) cheers .. stackoverflow.com/questions/21775023/…
    – Fattie
    Feb 14, 2014 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.