Hello
After 4 lose hours I realise that semaphores do not works in iOS
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
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 DISPATCH_TIME_FOREVER = 0. In iOS = ~0ull which means Unsigned Long Long with bitwise NOT, so highest possible 64 bit number.
b.r.