Skip to content

Commit 78cb06b

Browse files
docs: Add a new concurrency strategy example program
1 parent 289489a commit 78cb06b

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

examples/ex05_concurrency_policy.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
* Example: ex05_concurrency_policy
33
*
44
* Purpose:
5-
* Demonstrate the atomic concurrency policy path under multi-threaded
5+
* Demonstrate the fenced concurrency policy path under multi-threaded
66
* repeated dispatch.
77
*
88
* Expected results:
99
* - Concurrent add operations consistently produce value 42.
10+
* - Primitive load/store/CAS APIs work under fenced policy.
1011
* - mismatch_count remains zero after all worker threads join.
1112
* - Program prints a success message and exits with code 0.
1213
*/
@@ -21,13 +22,22 @@ import mcpplibs.primitives;
2122
using namespace mcpplibs::primitives;
2223

2324
int main() {
24-
// Point 5: Use atomic concurrency policy and verify concurrent consistency.
25-
using atomic_t =
26-
primitive<int, policy::value::checked, policy::concurrency::atomic,
25+
// Point 5: Use fenced concurrency policy and verify concurrent consistency.
26+
using fenced_t =
27+
primitive<int, policy::value::checked, policy::concurrency::fenced,
2728
policy::error::expected>;
2829

29-
auto const lhs = atomic_t{12};
30-
auto const rhs = atomic_t{30};
30+
auto const lhs = fenced_t{12};
31+
auto const rhs = fenced_t{30};
32+
33+
auto concurrent_value = fenced_t{1};
34+
concurrent_value.store(2);
35+
auto expected = 2;
36+
if (!concurrent_value.compare_exchange(expected, 3) ||
37+
concurrent_value.load() != 3) {
38+
std::cerr << "fenced load/store/CAS mismatch\n";
39+
return 1;
40+
}
3141

3242
std::atomic<int> mismatch_count{0};
3343
std::vector<std::thread> workers;
@@ -51,7 +61,7 @@ int main() {
5161

5262
// A non-zero mismatch count indicates unexpected behavior under concurrency.
5363
if (mismatch_count.load(std::memory_order_relaxed) != 0) {
54-
std::cerr << "atomic policy path mismatch\n";
64+
std::cerr << "fenced policy path mismatch\n";
5565
return 1;
5666
}
5767

0 commit comments

Comments
 (0)