29

I have a long running function inside an asynchronous (serial) worker queue. I know that sometimes this function hangs inside a particular openCV call. For some reason this hang is also causing the main thread to hang. When pausing and entering debug mode I see that there is a call to

semaphore_wait_trap()

on the main thread (Queue)

I can suspend the hanging thread (My worker queue) in debug mode and then this trap goes away and the GUI becomes responsive once again on the phone.

After unpausing the worker thread the GUI is responsive for 1-2 seconds (I suspect until this thread is activated again) and then the UI becomes unresponsive once again.

This thread makes no dispatch_sync() calls to the main thread/Queue

Is it possible that IOS pauses the main thread ("traps" it) because the worker is long running?

Can I force it to remove the block??

I am adding some print screens of the debug mode stack.

Before suspending the hanging Queue:

Main Queue Stack

And the hanging thread:

Hanging Queue

And After Pausing and suspending the bad queue:

After Suspending

Avba
  • 14,822
  • 20
  • 92
  • 192
  • Is "pixtr" you or a third party product? . If its third party i would consider detaching it from your UI to see if thats the problem – Warren Burton Oct 22 '15 at 14:57
  • There's not a lot to go on here (in particular, I don't know what's in `add_blob`), so the best I have is guesses. My suspicion is that you're passing incorrect flags to `cvFloodFill` and this causes it to hang locking up the GPU. Since UIKit also wants the GPU in order to compute scrolling (DYTransport is a private framework related to GPU computation), this hangs UIKit waiting for the GPU to become available. – Rob Napier Apr 29 '16 at 14:05

2 Answers2

2

Is it possible that IOS pauses the main thread ("traps" it) because the worker is long running? - NO. I think, your problem is related to drawing or changing some UI elements. Not all functions can be called from background thread (e.g. changes to UI elements has to be done in main thread.). In your serial queue, if any method needs to change UI elements, you have to call it on main thread e.g

dispatch_async(dispatch_get_main_queue(), ^{
                //do some main thread job here
            });
)
Muhammad Nabeel Arif
  • 19,140
  • 8
  • 51
  • 70
Fahri Azimov
  • 11,470
  • 2
  • 21
  • 29
  • I'm doing dispatch_queue_create("worker_queue", DISPATCH_QUEUE_SERIAL); – Avba Nov 20 '12 at 14:32
  • dispatch_async(my_queue, { worker block with callback on completion } ) – Avba Nov 20 '12 at 14:33
  • I'm using GPU on worker thread. Could that hang the main thread? If so how to do that? – Avba Nov 20 '12 at 20:51
  • 5
    I have used OpenCV just like you did, and there was a problem just like you are facing. In my case, the problem was that I was using Core Graphics functions on background thread. You have to provide the dispatch block code, and code of the methods you are calling in the block, so that we can help you. – Fahri Azimov Nov 21 '12 at 05:50
  • @FahriAzimov Do you know of a way to detect what line of code is causing it? Perhaps a symbolic breakpoint for semaphore? – Albert Renshaw Nov 05 '19 at 03:11
  • @AlbertRenshaw I usually traverse through threads to find out the cause. – Fahri Azimov Nov 06 '19 at 00:41
0

Maybe you just forgot to retain a variable into dispatch function call (As for me I was omitted a static keyword before dispatch_once_t declaration and dispatch can't process with inline function). The stack trace was just like yours. That was my fault.

+ (instancetype)sharedInstance
{
    (static was omitted) dispatch_once_t once;
    static id sharedInstance;
    dispatch_once(&once, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}