mootoh (owner)

Revisions

gist: 179668 Download_button fork
public
Public Clone URL: git://gist.github.com/179668.git
Embed All Files: show embed
dispatch_apply.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
#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);
 
   dispatch_queue_t the_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
 
   // 'idx' is zero indexed, just like:
   // for (idx = 0; idx < iterations; idx++)
   dispatch_apply(iterations, the_queue, ^(size_t idx) {
      long wait_time = (random() % PRECISION) * 100000;
      usleep(wait_time);
      printf("%zu\n", idx);
   });
   return 0;
}