Compare commits

..

No commits in common. "lesson_6" and "master" have entirely different histories.

5 changed files with 0 additions and 259 deletions

View File

@ -1,37 +0,0 @@
/*
* 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';
}
};

View File

@ -1,53 +0,0 @@
/*
* 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();
}

View File

@ -1,92 +0,0 @@
/*
* 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();
}

View File

@ -1,59 +0,0 @@
/*
* 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();
}
}

View File

@ -1,18 +0,0 @@
/*
* 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;
}