Semaphores do not work

Hello

After 4 lose hours I realise that semaphores do not works in iOS :frowning:

Code is C#

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), delegate()
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), delegate()
    {
        sleep(5);
        dispatch_semaphore_signal(semaphore);
        NSLog("signaling");
    });
    NSLog("wait");
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    NSLog("finish");
});

wrong order output:

wait
finish
signaling

code in Objective-C:

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

        sleep(5);
        dispatch_semaphore_signal(semaphore);
        NSLog(@"signaling");
    });
    
    NSLog(@"wait");
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    NSLog(@"finish");
});

correct order output:

wait
signaling
finish

[EDIT]

Cannot sleep so did some other idea :smile:

If dispatch_semaphore_wait used with timeout, then is working:

dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, (10 * NSEC_PER_SEC)));

So problem must be with DISPATCH_TIME_FOREVER ?

[EDIT2]

Found problem :slight_smile: DISPATCH_TIME_FOREVER = 0. In iOS = ~0ull which means Unsigned Long Long with bitwise NOT, so highest possible 64 bit number.

b.r.

Ouch. Sounds like an fxgen importer bug.

Thanks, logged as bugs://72727

bugs://72727 got closed with status fixed.

1 Like

This didnโ€™t make the cut for the beta weโ€™re building now (a full import run takes a few hours), but you can use -1 (0xFFFFFFFFFFFFFFFF) as a workaround for now.

I used ULLONG_MAX :smile:

1 Like