-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
74 lines (60 loc) · 1.82 KB
/
example.cpp
File metadata and controls
74 lines (60 loc) · 1.82 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <type_traits>
#include <vector>
#include <thread>
#include <ranges>
#include <format>
#include <cassert>
#include "agano.hpp"
void test1() noexcept {
auto counter1 = agano::make_thread_bound<int>(42);
std::thread t{
[&] {
auto counter2 = counter1;
assert(counter1.get_owner_id() != counter2.get_owner_id());
auto counter3 = std::move(counter1);
// here use-after-move occurred. Remove bugprone-use-after-move from WarningsAsErrors section
// in .clang-tidy file for test purposes
assert(counter1.is_unbound());
assert(counter2.get_owner_id() == counter3.get_owner_id());
}
};
t.join();
}
void test2() noexcept {
auto counter = agano::make_thread_bound<int>(agano::defer_binding, 42);
std::thread t{
[counter1 = std::move(counter)]() mutable {
auto counter2 = counter1;
assert(counter1.get_owner_id() == counter2.get_owner_id());
assert(counter1.is_unbound());
*counter1 += 50;
assert(counter1.get_owner_id() == std::this_thread::get_id());
assert(counter2.is_unbound());
assert(*counter2 == 42);
assert(!counter2.is_unbound());
auto counter3 = std::move(counter1);
assert(counter1.is_unbound());
assert(counter2.get_owner_id() == counter3.get_owner_id());
}
};
t.join();
}
void intentional_error() noexcept {
auto counter = agano::make_thread_bound<int>(0);
*counter += 10;
std::thread t{
[&] {
std::cout << *counter << "\n";
}
};
*counter += 10;
std::cout << *counter << "\n";
t.join();
}
int main () {
test1();
test2();
intentional_error();
return 0;
}