lesson_6 #6

Open
alexander wants to merge 4 commits from lesson_6 into master
5 changed files with 259 additions and 0 deletions

37
lesson_6/Timer.hpp Normal file
View File

@ -0,0 +1,37 @@
/*
* Timer.hpp
*
* Created on: 12 окт. 2021 г.
* Author: alexander
*/
#pragma once
#include <iostream>
#include <chrono>
class Timer
{
private:
using clock_t = std::chrono::high_resolution_clock;
using second_t = std::chrono::duration<double, std::ratio<1> >;
std::string m_name;
std::chrono::time_point<clock_t> m_beg;
double elapsed() const
{
return std::chrono::duration_cast<second_t>(clock_t::now() -m_beg).count();
}
public:
Timer() : m_beg(clock_t::now()) { }
Timer(std::string name) : m_name(name), m_beg(clock_t::now()) { }
void start(std::string name) {
m_name = name;
m_beg = clock_t::now();
}
void print() const {
std::cout << m_name << ":\t" << elapsed() * 1000 << " ms" << '\n';
}
};

53
lesson_6/exercise_1.hpp Normal file
View File

@ -0,0 +1,53 @@
/*
* exercise_1.hpp
*
* Created on: 10 окт. 2021 г.
* Author: alexander
*/
#pragma once
#include <iostream>
#include <thread>
#include <mutex>
static std::mutex mtx_cout;
class pcout
{
private:
std::lock_guard<std::mutex> lg;
public:
pcout() : lg(std::lock_guard<std::mutex>(mtx_cout))
{
}
template<typename T>
pcout& operator<<(const T &data)
{
std::cout << data;
return *this;
}
pcout& operator<<(std::ostream& (*fp)(std::ostream&)) // т.к. std::endl является функцией,
{ // то для неё делаем перегрузку operator<<
std::cout << fp; // которая принимает указатель на функцию
return *this; // типа std::ostream& и возвращает наш
} // защищённый поток вывода
};
void doSomething(int number)
{
pcout() << "start thread " << number << std::endl;
pcout() << "stop thread " << number << std::endl;
}
void exercise_1()
{
std::thread th1(doSomething, 1);
std::thread th2(doSomething, 2);
std::thread th3(doSomething, 3);
th1.join();
th2.join();
th3.join();
}

92
lesson_6/exercise_2.hpp Normal file
View File

@ -0,0 +1,92 @@
/*
* exercise_2.hpp
*
* Created on: 11 окт. 2021 г.
* Author: alexander
*/
#pragma once
#include <iostream>
#include <thread>
#include <vector>
#include <mutex>
#include "Timer.hpp"
bool isPrime(size_t num)
{
size_t limit = num / 2;
if (num > 2)
{
for (size_t i = 2; i <= limit; ++i)
{
if (num % i == 0)
{
return false;
}
}
}
return true;
}
void exercise_2()
{
size_t maxValue = 500000;
size_t counThreads = 30;
std::vector<size_t> answers;
std::mutex m;
Timer timer("With threads");
for (size_t i = 0; i < maxValue;)
{
std::vector<std::thread> v;
for (size_t j = i + counThreads; i <= j; ++i)
{
v.push_back(std::thread([=, &m, &answers]()
{
if (isPrime(i))
{
std::lock_guard lg(m);
answers.push_back(i);
}
}));
}
for (auto &t : v)
{
t.join();
}
}
// for (const auto &a : answers)
// {
// std::cout << "Число " << a << " простое" << std::endl;
// }
timer.print();
answers.clear();
answers.shrink_to_fit();
timer.start("Without threads");
for (size_t i = 0; i < maxValue; ++i)
{
if (isPrime(i))
{
answers.push_back(i);
}
}
// for (const auto &a : answers)
// {
// std::cout << "Число " << a << " простое" << std::endl;
// }
timer.print();
}

59
lesson_6/exercise_3.hpp Normal file
View File

@ -0,0 +1,59 @@
/*
* exercise_3.hpp
*
* Created on: 12 окт. 2021 г.
* Author: alexander
*/
#pragma once
#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <algorithm>
#include <random>
std::mutex m;
void generate_things(std::vector<int> &v)
{
std::random_device rd;
std::mt19937 mersenne(rd());
std::uniform_int_distribution<int> urd(0, 1000);
std::lock_guard lg(m);
std::generate(v.begin(), v.end(), [&]()
{
return urd(mersenne);
});
}
void pop_thing(std::vector<int> &v)
{
std::lock_guard lg(m);
std::cout << *std::max_element(v.begin(), v.end()) << std::endl;
}
void exercise_3()
{
std::vector<int> v(100);
size_t count = 100;
while (count--)
{
std::thread owner([&]()
{
generate_things(v);
});
std::thread thief([&]()
{
pop_thing(v);
});
owner.join();
thief.join();
}
}

18
lesson_6/main.cpp Normal file
View File

@ -0,0 +1,18 @@
/*
* main.cpp
*
* Created on: 10 окт. 2021 г.
* Author: alexander
*/
#include "exercise_1.hpp"
#include "exercise_2.hpp"
#include "exercise_3.hpp"
int main()
{
// exercise_1();
// exercise_2();
exercise_3();
return 0;
}