1 // RUN: %clang_tsan %s -o %t
2 // RUN: %run %t 2>&1 | FileCheck %s --implicit-check-not='ThreadSanitizer'
4 #include <dispatch/dispatch.h>
12 void callback(void *context
, size_t i
) {
15 barrier_wait(&barrier
);
18 int main(int argc
, const char *argv
[]) {
19 fprintf(stderr
, "start\n");
21 // Warm up GCD (workaround for macOS Sierra where dispatch_apply might run single-threaded).
22 dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0), ^{ });
24 dispatch_queue_t q
= dispatch_queue_create("my.queue", DISPATCH_QUEUE_CONCURRENT
);
28 barrier_init(&barrier
, size
);
29 dispatch_apply(size
, q
, ^(size_t i
) {
32 barrier_wait(&barrier
);
35 for (size_t i
= 0; i
< size
; i
++) {
36 fprintf(stderr
, "array[%ld] = %ld\n", i
, array
[i
]);
41 barrier_init(&barrier
, size
);
42 dispatch_apply_f(size
, q
, NULL
, &callback
);
44 for (size_t i
= 0; i
< size
; i
++) {
45 fprintf(stderr
, "array[%ld] = %ld\n", i
, array
[i
]);
48 fprintf(stderr
, "done\n");
53 // CHECK: array[0] = 42
54 // CHECK: array[1] = 43
55 // CHECK: array[0] = 142
56 // CHECK: array[1] = 143