Skip to content

Commit

Permalink
[CPP20]Add std::barrier examples
Browse files Browse the repository at this point in the history
  • Loading branch information
wtffqbpl committed Dec 26, 2023
1 parent d115f17 commit 9d468d5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ add_executable(cpp_weekly

cpp20_features/comparisons.cc
cpp20_features/latches.cc
cpp20_features/barrier_test.cc

cpp_challenges/chap1_math_problems.cc

Expand Down
56 changes: 56 additions & 0 deletions cpp20_features/barrier_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <barrier>
#include <cmath>
#include <format>
#include <gtest/gtest.h>
#include <iostream>
#include <stop_token>
#include <thread>
#include <vector>

namespace {

void barrier_test() {
// initialize and print a collection of floating-point values:
std::vector values{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0};

// define a lambda function that prints all values.
// -NOTE: has to be noexcept to be used as barrier callback
auto print_values = [&values]() noexcept {
for (auto val : values) {
std::cout << std::format("{:<7.5}", val);
}
std::cout << '\n';
};

// print initial values:
print_values();

// initialize a barrier that prints the values when all threads have done
// their computations:
std::barrier all_done{
int(values.size()), // initial value of the counter
print_values}; // callback to call whenever the counter is 0

// initialize a thread for each value to compute its square root in a loop:
std::vector<std::thread> threads;
threads.reserve(values.size());

for (auto &value : values) {
threads.emplace_back([&value, &values, &all_done] {
// repeatedly:
for (int i = 0; i < 5; ++i) {
// compute square root:
value = std::sqrt(value);
// and synchronize with other threads to print values:
all_done.arrive_and_wait();
}
});
}

for (auto &t : threads)
t.join();
}

} // namespace

TEST(barrier_test, basic_test) { barrier_test(); }

0 comments on commit 9d468d5

Please sign in to comment.