Skip to content

Instantly share code, notes, and snippets.

@lemire
Created September 16, 2023 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lemire/6a991b1de13368cd688fcc3611ee7478 to your computer and use it in GitHub Desktop.
Save lemire/6a991b1de13368cd688fcc3611ee7478 to your computer and use it in GitHub Desktop.
template <int align> struct alignas(align) A {
volatile uint64_t count;
};
constexpr size_t iterations = 100'000'000;
void counter(volatile uint64_t *counterpt) {
for (size_t i = 0; i < iterations; i++) {
*counterpt = 3 * *counterpt * *counterpt + 1;
}
}
template <int align> uint64_t threaded_counter() {
A<align> a[4];
static_assert(sizeof(a) == 4 * align);
a[0].count = 1;
a[1].count = 2;
a[2].count = 3;
a[3].count = 4;
std::thread t1(counter, &a[0].count);
std::thread t2(counter, &a[1].count);
std::thread t3(counter, &a[2].count);
std::thread t4(counter, &a[3].count);
t1.join();
t2.join();
t3.join();
t4.join();
return a[0].count + a[1].count + a[2].count + a[3].count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment