Skip to content

Commit 9483593

Browse files
Examples (#81)
* Allow tsan in container * Details for move * Remove basic example * Refine streaming * Describe move * Describe semaphore * Use static_channel in move * Remove multithreading_static_channel * Remove multithreading
1 parent 790e10a commit 9483593

File tree

10 files changed

+98
-216
lines changed

10 files changed

+98
-216
lines changed

.devcontainer/devcontainer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,10 @@
2323
"source=cache,target=/home/ubuntu/.cache,type=volume"
2424
],
2525
"workspaceFolder": "/workspace",
26+
"runArgs": [
27+
"--cap-add=SYS_PTRACE",
28+
"--security-opt",
29+
"seccomp=unconfined"
30+
],
2631
"remoteUser": "ubuntu"
2732
}

examples/.clang-tidy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ InheritParentConfig: true
22

33
Checks: >
44
-cppcoreguidelines-avoid-magic-numbers,
5-
-readability-magic-numbers
5+
-readability-magic-numbers,
6+
-fuchsia-default-arguments-calls

examples/CMakeLists.txt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,12 @@ add_custom_target(examples)
2626
add_custom_target(run_examples)
2727

2828
# Examples
29-
add_example(example_basic basic.cpp)
30-
run_example(example_basic)
31-
32-
add_example(example_close close.cpp)
33-
run_example(example_close)
34-
3529
add_example(example_move move.cpp)
3630
run_example(example_move)
3731

38-
add_example(example_multithreading multithreading.cpp)
39-
4032
add_example(example_streaming streaming.cpp)
4133
run_example(example_streaming)
4234

43-
add_example(example_multithreading_static_channel multithreading_static_channel.cpp)
44-
run_example(example_multithreading_static_channel)
45-
4635
add_example(example_concurrent_map_filter concurrent_map_filter.cpp)
4736
run_example(example_concurrent_map_filter)
4837

examples/basic.cpp

Lines changed: 0 additions & 21 deletions
This file was deleted.

examples/close.cpp

Lines changed: 0 additions & 58 deletions
This file was deleted.

examples/move.cpp

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,126 @@
1-
#include <msd/channel.hpp>
1+
#include <msd/static_channel.hpp>
22

3+
#include <cstddef>
34
#include <iostream>
5+
#include <utility>
6+
#include <vector>
47

58
class data final {
6-
int value_{};
7-
89
public:
10+
static std::size_t copies_;
11+
static std::size_t moves_;
12+
913
data() = default;
1014
explicit data(int value) : value_{value} {}
1115

1216
int get_value() const { return value_; }
1317

14-
data(const data& other) noexcept : value_{other.value_} { std::cout << "copy " << value_ << '\n'; }
18+
data(const data& other) noexcept : value_{other.value_}
19+
{
20+
std::cout << "copy " << value_ << '\n';
21+
++copies_;
22+
}
23+
1524
data& operator=(const data& other)
1625
{
1726
if (this != &other) {
1827
value_ = other.value_;
28+
std::cout << "copy " << value_ << '\n';
29+
++copies_;
1930
}
20-
std::cout << "copy " << value_ << '\n';
2131

2232
return *this;
2333
}
2434

25-
data(data&& other) noexcept : value_{other.value_} { std::cout << "move " << value_ << '\n'; }
35+
data(data&& other) noexcept : value_{other.value_}
36+
{
37+
std::cout << "move " << value_ << '\n';
38+
++moves_;
39+
}
40+
2641
data& operator=(data&& other) noexcept
2742
{
2843
if (this != &other) {
2944
value_ = other.value_;
3045
std::cout << "move " << value_ << '\n';
46+
++moves_;
3147
}
3248

3349
return *this;
3450
}
3551

3652
~data() = default;
53+
54+
private:
55+
int value_{};
3756
};
3857

58+
std::size_t data::copies_{};
59+
std::size_t data::moves_{};
60+
61+
// Copy and move semantics with a user-defined type.
62+
3963
int main()
4064
{
41-
msd::channel<data> chan{10};
65+
msd::static_channel<data, 10> chan{};
4266

43-
auto in1 = data{1};
67+
// l-value: will be copied
68+
const auto in1 = data{1};
4469
chan << in1;
4570

71+
// r-value: will be moved
4672
chan << data{2};
4773

74+
// l-value -> std::move -> r-value: will be moved
4875
auto in3 = data{3};
4976
chan << std::move(in3);
5077

51-
for (const auto& out : chan) {
78+
std::vector<int> actual;
79+
80+
// Each value will be moved when read
81+
for (const data& out : chan) {
5282
std::cout << out.get_value() << '\n';
5383

84+
actual.push_back(out.get_value());
85+
5486
if (chan.empty()) {
5587
break;
5688
}
5789
}
90+
91+
// Read values
92+
const std::vector<int> expected{1, 2, 3};
93+
94+
if (actual != expected) {
95+
std::cerr << "Error: got: ";
96+
for (const int value : actual) {
97+
std::cerr << value << ", ";
98+
}
99+
100+
std::cerr << ", expected: ";
101+
for (const int value : expected) {
102+
std::cerr << value << ", ";
103+
}
104+
std::cerr << '\n';
105+
106+
std::terminate();
107+
}
108+
109+
// 1 copy when in1 was written
110+
constexpr std::size_t expected_copies = 1;
111+
112+
if (data::copies_ != expected_copies) {
113+
std::cerr << "Error: copies: " << data::copies_ << ", expected: " << expected_copies << '\n';
114+
std::terminate();
115+
}
116+
117+
// 1 move when the second value was written
118+
// 1 move when in3 was written
119+
// 3 moves when the 3 values were read
120+
constexpr std::size_t expected_moves = 5;
121+
122+
if (data::moves_ != expected_moves) {
123+
std::cerr << "Error: moves: " << data::moves_ << ", expected: " << expected_moves << '\n';
124+
std::terminate();
125+
}
58126
}

examples/multithreading.cpp

Lines changed: 0 additions & 46 deletions
This file was deleted.

examples/multithreading_static_channel.cpp

Lines changed: 0 additions & 60 deletions
This file was deleted.

examples/semaphore.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ int simulate_heavy_computation(const int value)
2929
return result;
3030
};
3131

32+
// https://en.wikipedia.org/wiki/Semaphore_(programming)
33+
3234
int main()
3335
{
3436
semaphore sem{2};

0 commit comments

Comments
 (0)