Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.mm
- // dispatch_test
- //
- // Created by Antony Searle on 20/9/19.
- // Copyright © 2019 Antony Searle. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #include <atomic>
- #include <thread>
- int main(int argc, const char * argv[]) {
- // std::atomic<long> x(0);
- auto N = 1000;
- auto M = 100'000'000;
- auto active = new std::atomic<long>(0);
- auto barrier = new std::atomic<long>(N);
- printf("concurrency %u\n", std::thread::hardware_concurrency());
- auto q = dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0);
- @autoreleasepool {
- for (long i = 0; i != N; ++i) {
- // auto q = dispatch_queue_create(nullptr, DISPATCH_QUEUE_CONCURRENT);
- // auto q = dispatch_queue_create(nullptr, DISPATCH_QUEUE_SERIAL);
- dispatch_async(q, ^{
- long z = active->fetch_add(1, std::memory_order_acq_rel);
- // std::this_thread::sleep_for(std::chrono::seconds(1));
- for (auto j = 0; j != M; ++j) // do some work
- z *= z;
- printf("%ld\t%ld/%ld\n", active->fetch_sub(1, std::memory_order_acq_rel), i, z);
- if (barrier->fetch_sub(1, std::memory_order_acq_rel) == 1)
- exit(EXIT_SUCCESS);
- });
- printf("submitted block %ld\n", i);
- }
- dispatch_main();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement