#include <dispatch/dispatch.h>
static void do_all_work (dispatch_group_t group, dispatch_queue_t main_queue, dispatch_queue_t bg_queue)
{
for (int i = 1; i <= 10; ++i) {
dispatch_group_async (group, main_queue, ^{
printf ("Starting task #%d\n", i);
dispatch_group_async (group, bg_queue, ^{
double sum = 0;
double limit = i * 10000;
for (double k = 1.0; k <= limit; k += 1) sum += 1.0 / k;
dispatch_group_async (group, main_queue, ^{
printf ("Task #%d: result = %.3f\n", i, sum);
});
});
});
}
}
int main (int argc, const char * argv[])
{
dispatch_queue_t bg_queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t main_queue = dispatch_get_main_queue ();
dispatch_group_t group = dispatch_group_create();
do_all_work (group, main_queue, bg_queue);
dispatch_async (bg_queue, ^{
dispatch_group_wait (group, DISPATCH_TIME_FOREVER);
dispatch_release (group);
dispatch_async(main_queue, ^{ exit (0); });
});
dispatch_main ();
return 0;
}