-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_main.cc
More file actions
48 lines (34 loc) · 795 Bytes
/
test_main.cc
File metadata and controls
48 lines (34 loc) · 795 Bytes
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <cassert>
#include <atomic>
#include "dispatch-c++.h"
void test_dispatch_queue()
{
std::atomic<int> a;
a = 0;
dispatch::queue queue = dispatch::queue::globalQueue(dispatch::queue::priority::DEFAULT);
queue.apply(100, [&a] (size_t idx) {
a += idx + 1;
});
assert(a == 5050);
}
void test_dispatch_group()
{
int a = 0;
dispatch::group group;
dispatch::queue queue("main_test.dispatch_async", dispatch::queue::attr::SERIAL);
for (int i = 1; i <= 100; i++) {
group.async(queue, [=, &a] () {
a += i;
});
}
group.wait(DISPATCH_TIME_FOREVER);
assert(a == 5050);
}
int test_main(const int argc, const char * argv[], const char * envp[])
{
test_dispatch_queue();
test_dispatch_group();
std::cout << "Success!" << std::endl;
return 0;
}