Why will the following simple grand central dispatch program not work correctly?
grand-central-dispatch, objective-c
Solution
You have to call `dispatch_main()` if your program does not have an event loop:
int main(int argc, char **argv)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
printf("Done outer async\n");
dispatch_async(dispatch_get_main_queue(),^{
printf("Done inner sync");
});
});
dispatch_main();
return 0;
}
From the documentation:
This function "parks" the main thread and waits for blocks to be submitted to the main queue. Applications that call `UIApplicationMain` (iOS), `NSApplicationMain` (Mac OS X), or `CFRunLoopRun` on the main thread must not call `dispatch_main`.
Problem
So I was expecting the following program to print two lines. However it doesn't print anything. Any ideas on what needs to be fixed? ``` #import <Foundation/Foundation.h> #import <dispatch/dispatch.h> int main(int argc, char **argv) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ printf("Done outer async\n"); dispatch_async(dispatch_get_main_queue(),^{ printf("Done inner sync"); }); }); return 0; } ``` Thanks