1 .\" Copyright (c) 2008-2010 Apple Inc. All rights reserved.
7 .Nd schedule blocks for iterative execution
9 .Fd #include <dispatch/dispatch.h>
12 .Fa "size_t iterations" "dispatch_queue_t queue" "void (^block)(size_t)"
16 .Fa "size_t iterations" "dispatch_queue_t queue" "void *context" "void (*function)(void *, size_t)"
21 function provides data-level concurrency through a "for (;;)" loop like primitive:
23 dispatch_queue_t the_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
24 size_t iterations = 10;
26 // 'idx' is zero indexed, just like:
27 // for (idx = 0; idx < iterations; idx++)
29 dispatch_apply(iterations, the_queue, ^(size_t idx) {
30 printf("%zu\\n", idx);
34 Like a "for (;;)" loop, the
36 function is synchronous.
37 If asynchronous behavior is desired, please wrap the call to
41 against another queue.
43 Sometimes, when the block passed to
45 is simple, the use of striding can tune performance.
46 Calculating the optimal stride is best left to experimentation.
47 Start with a stride of one and work upwards until the desired performance is
48 achieved (perhaps using a power of two search):
52 dispatch_apply(count / STRIDE, queue, ^(size_t idx) {
53 size_t j = idx * STRIDE;
54 size_t j_stop = j + STRIDE;
56 printf("%zu\\n", j++);
61 for (i = count - (count % STRIDE); i < count; i++) {
65 .Sh IMPLIED REFERENCES
66 Synchronous functions within the dispatch framework hold an implied reference
67 on the target queue. In other words, the synchronous function borrows the
68 reference of the calling function (this is valid because the calling function
69 is blocked waiting for the result of the synchronous function, and therefore
70 cannot modify the reference count of the target queue until after the
71 synchronous function has returned).
73 This is in contrast to asynchronous functions which must retain both the block
74 and target queue for the duration of the asynchronous operation (as the calling
75 function may immediately release its interest in these objects).
79 is a convenient wrapper around
81 and a semaphore to wait for completion.
82 In practice, the dispatch library optimizes this function.
86 function is a wrapper around
87 .Fn dispatch_apply_f .
93 is expected to be either independent or dependent
95 on work already performed in lower-indexed invocations of the block. If
96 the block's index dependency is non-linear, it is recommended to
97 use a for-loop around invocations of
101 .Xr dispatch_async 3 ,
102 .Xr dispatch_queue_create 3 ,
103 .Xr dispatch_semaphore_create 3