Skip to content

Instantly share code, notes, and snippets.

@plusangel
Created July 13, 2019 20:00
Show Gist options
  • Save plusangel/2150fed163a7a5773bd53f57aad31960 to your computer and use it in GitHub Desktop.
Save plusangel/2150fed163a7a5773bd53f57aad31960 to your computer and use it in GitHub Desktop.
c++ thread experiments #1
#include <iostream>
#include <vector>
#include <thread>
#include <future>
std::mutex g_display_mutex;
// function for threads
void accumulator_function(const std::vector<int> &v, unsigned long long &acm,
size_t begin, size_t end)
{
acm = 0;
for (size_t i = begin; i < end; ++i)
{
acm += v[i];
// lock guard
/*
std::lock_guard<std::mutex> guard(g_display_mutex);
std::thread::id this_id = std::this_thread::get_id();
std::cout << "My thread id is: " << this_id << std::endl;
*/
// old fashioned mutex
/*
g_display_mutex.lock();
std::thread::id this_id = std::this_thread::get_id();
std::cout << "My thread id is: " << this_id << std::endl;
g_display_mutex.unlock();
*/
}
}
// functor
class Accumulator_functor {
public:
void operator()(const std::vector<int> &v,
size_t begin, size_t end)
{
acm_ = 0;
for (size_t i = begin; i < end; ++i)
{
acm_ += v[i];
}
}
unsigned long long acm_;
};
int main() {
size_t c = std::thread::hardware_concurrency();
std::cout << "number of cores: " << c << std::endl;
std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
unsigned long long acm1{0};
unsigned long long acm2{0};
std::thread t1(accumulator_function, std::ref(v), std::ref(acm1), 0, v.size()/2);
std::thread t2(accumulator_function, std::ref(v), std::ref(acm2), v.size()/2, v.size());
t1.join();
t2.join();
std::cout << "function example:" << std::endl;
std::cout << "acm1: " << acm1 << std::endl;
std::cout << "acm2: " << acm2 << std::endl;
std::cout << "acm1 + acm2: " << acm1 + acm2 << std::endl;
std::cout << "--------------" << std::endl;
Accumulator_functor acc1 = Accumulator_functor();
Accumulator_functor acc2 = Accumulator_functor();
std::thread t3(std::ref(acc1), std::ref(v), 0, v.size()/2);
std::thread t4(std::ref(acc2), std::ref(v), v.size() / 2, v.size());
t3.join();
t4.join();
std::cout << "functor example:" << std::endl;
std::cout << "acm1: " << acc1.acm_ << std::endl;
std::cout << "acm2: " << acc2.acm_ << std::endl;
std::cout << "acm1 + acm2: " << acc1.acm_ + acc2.acm_ << std::endl;
std::cout << "--------------" << std::endl;
// lambdas
unsigned long long acm5 = 0;
unsigned long long acm6 = 0;
std::thread t5([&acm5, &v] {
for (size_t i = 0; i < v.size()/2; ++i)
{
acm5 += v[i];
}
});
std::thread t6([&acm6, &v] {
for (size_t i = v.size() / 2; i < v.size(); ++i)
{
acm6 += v[i];
}
});
t5.join();
t6.join();
std::cout << "lambdas example:" << std::endl;
std::cout << "acm1: " << acm5 << std::endl;
std::cout << "acm2: " << acm6 << std::endl;
std::cout << "acm1 + acm2: " << acm5 + acm6 << std::endl;
std::cout << "--------------" << std::endl;
auto f1 = [](const std::vector<int> &v,
size_t begin, size_t end)
{
unsigned long long acm = 0;
for (size_t i = begin; i < end; ++i)
{
acm += v[i];
}
return acm;
};
auto t7 = std::async(f1, std::ref(v), 0, v.size()/2);
auto t8 = std::async(f1, std::ref(v), v.size() / 2, v.size());
unsigned long long acm7 = t7.get();
unsigned long long acm8 = t8.get();
std::cout << "tasks example:" << std::endl;
std::cout << "acm1: " << acm7 << std::endl;
std::cout << "acm2: " << acm8 << std::endl;
std::cout << "acm1 + acm2: " << acm7 + acm8 << std::endl;
std::cout << "--------------" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment