Skip to content
Snippets Groups Projects
Commit ab452601 authored by Sandipan Mohanty's avatar Sandipan Mohanty
Browse files

Add missing threads examples

parent 040d81c4
No related branches found
No related tags found
No related merge requests found
#include "timeit.hh"
#include <array>
#include <iostream>
#include <thread>
constexpr auto WORKLOAD = 1000000000UL;
constexpr auto PARALLEL = 16UL;
struct wrapped1 {
int val {};
};
struct alignas(std::hardware_destructive_interference_size) wrapped2 {
int val {};
};
template <class W>
struct func {
void operator()(volatile W* var)
{
for (unsigned i = 0; i < WORKLOAD / PARALLEL; ++i) {
var->val = var->val + 1;
}
}
};
auto main() -> int
{
timeit("Different threads accumulating to different variables, but different cache lines",
5UL,
[] {
std::array<wrapped2, PARALLEL> arr {};
{
std::array<std::jthread, PARALLEL> threads;
for (unsigned i = 0U; i < PARALLEL; ++i) {
threads[i] = std::jthread(func<wrapped2>{}, &arr[i]);
}
}
});
timeit("Different threads accumulating to different variables, but in the same cache line",
5UL,
[] {
std::array<wrapped1, PARALLEL> arr {};
{
std::array<std::jthread, PARALLEL> threads;
for (unsigned i = 0U; i < PARALLEL; ++i) {
threads[i] = std::jthread(func<wrapped1>{}, &arr[i]);
}
}
});
}
#include <cmath>
#include <iostream>
#include <numbers>
#include <thread>
auto main() -> int
{
using std::numbers::pi;
constexpr auto N = 1'000'000UL;
std::jthread j1 { [=]() {
auto tot1 = 0.;
for (auto i = 0UL; i < N; ++i) {
auto ang = 2 * i * pi / N;
tot1 += std::cos(ang) * std::cos(ang);
}
std::cout << "Thread 1 got total " << tot1 << "\n";
} };
std::jthread j2 { [=]() {
auto tot2 = 0.;
for (auto i = 0UL; i < N; ++i) {
auto ang = 2 * i * pi / N;
tot2 += std::sin(ang) * std::sin(ang);
}
std::cout << "Thread 2 got total " << tot2 << "\n";
} };
}
#include <cmath>
#include <iostream>
#include <numbers>
#include <thread>
auto main() -> int
{
using std::numbers::pi;
constexpr auto N = 1'000'000UL;
auto tot = 0.;
{
std::jthread j1 { [&]() {
for (auto i = 0UL; i < N; ++i) {
auto ang = 2 * i * pi / N;
tot += std::cos(ang) * std::cos(ang);
}
} };
std::jthread j2 { [&]() {
for (auto i = 0UL; i < N; ++i) {
auto ang = 2 * i * pi / N;
tot += std::sin(ang) * std::sin(ang);
}
} };
}
std::cout << "Total " << tot << "\n";
}
#include <cmath>
#include <iostream>
#include <mutex>
#include <numbers>
#include <thread>
auto main() -> int
{
using std::numbers::pi;
constexpr auto N = 1'000'000UL;
auto tot = 0.;
std::mutex totmutex;
{
std::jthread j1 { [&]() {
for (auto i = 0UL; i < N; ++i) {
auto ang = 2 * i * pi / N;
std::scoped_lock lck { totmutex };
tot += std::cos(ang) * std::cos(ang);
}
} };
std::jthread j2 { [&]() {
for (auto i = 0UL; i < N; ++i) {
auto ang = 2 * i * pi / N;
std::scoped_lock lck { totmutex };
tot += std::sin(ang) * std::sin(ang);
}
} };
}
std::cout << "Total " << tot << "\n";
}
#include <atomic>
#include <cmath>
#include <iostream>
#include <numbers>
#include <thread>
auto main() -> int
{
using std::numbers::pi;
constexpr auto N = 1'000'000UL;
std::atomic<double> tot {};
{
std::jthread j1 { [&]() {
for (auto i = 0UL; i < N; ++i) {
auto ang = 2 * i * pi / N;
tot += std::cos(ang) * std::cos(ang);
}
} };
std::jthread j2 { [&]() {
for (auto i = 0UL; i < N; ++i) {
auto ang = 2 * i * pi / N;
tot += std::sin(ang) * std::sin(ang);
}
} };
}
std::cout << "Total " << tot << "\n";
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment