mootoh (owner)

Revisions

gist: 179670 Download_button fork
public
Public Clone URL: git://gist.github.com/179670.git
Embed All Files: show embed
dispatch_apply-async.c #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dispatch/dispatch.h>
 
#define PRECISION 10
 
int main(int argc, char **argv)
{
   size_t iterations = 10;
   srandom(3);
 
   // make references to 2 distinct queues.
   dispatch_queue_t the_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
   dispatch_queue_t other_queue = dispatch_queue_create("other_queue", NULL);
 
   // 'idx' is zero indexed, just like:
   // for (idx = 0; idx < iterations; idx++)
   dispatch_async(other_queue, ^{
      dispatch_apply(iterations, the_queue, ^(size_t idx) {
         long wait_time = (random() % PRECISION) * 100000;
         usleep(wait_time);
         printf("%zu\n", idx);
      });
   });
 
   sleep(10); // wait for dispatch_apply to finish
   return 0;
}