-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); } |