diff --git a/lesson_6/Timer.hpp b/lesson_6/Timer.hpp new file mode 100644 index 0000000..ef85f67 --- /dev/null +++ b/lesson_6/Timer.hpp @@ -0,0 +1,37 @@ +/* + * Timer.hpp + * + * Created on: 12 окт. 2021 г. + * Author: alexander + */ + +#pragma once + +#include +#include + +class Timer +{ +private: + using clock_t = std::chrono::high_resolution_clock; + using second_t = std::chrono::duration >; + + std::string m_name; + std::chrono::time_point m_beg; + double elapsed() const + { + return std::chrono::duration_cast(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'; + } +}; diff --git a/lesson_6/exercise_1.hpp b/lesson_6/exercise_1.hpp new file mode 100644 index 0000000..cb97401 --- /dev/null +++ b/lesson_6/exercise_1.hpp @@ -0,0 +1,53 @@ +/* + * exercise_1.hpp + * + * Created on: 10 окт. 2021 г. + * Author: alexander + */ + +#pragma once + +#include +#include +#include + +static std::mutex mtx_cout; + +class pcout +{ +private: + std::lock_guard lg; +public: + pcout() : lg(std::lock_guard(mtx_cout)) + { + } + + template + 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(); +} diff --git a/lesson_6/exercise_2.hpp b/lesson_6/exercise_2.hpp new file mode 100644 index 0000000..0a019ba --- /dev/null +++ b/lesson_6/exercise_2.hpp @@ -0,0 +1,92 @@ +/* + * exercise_2.hpp + * + * Created on: 11 окт. 2021 г. + * Author: alexander + */ + +#pragma once + +#include +#include +#include +#include +#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 answers; + std::mutex m; + + Timer timer("With threads"); + + for (size_t i = 0; i < maxValue;) + { + std::vector 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(); +} diff --git a/lesson_6/exercise_3.hpp b/lesson_6/exercise_3.hpp new file mode 100644 index 0000000..87a69b0 --- /dev/null +++ b/lesson_6/exercise_3.hpp @@ -0,0 +1,59 @@ +/* + * exercise_3.hpp + * + * Created on: 12 окт. 2021 г. + * Author: alexander + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +std::mutex m; + +void generate_things(std::vector &v) +{ + std::random_device rd; + std::mt19937 mersenne(rd()); + std::uniform_int_distribution urd(0, 1000); + + std::lock_guard lg(m); + + std::generate(v.begin(), v.end(), [&]() + { + return urd(mersenne); + }); +} + +void pop_thing(std::vector &v) +{ + std::lock_guard lg(m); + + std::cout << *std::max_element(v.begin(), v.end()) << std::endl; +} + +void exercise_3() +{ + std::vector v(100); + size_t count = 100; + + while (count--) + { + std::thread owner([&]() + { + generate_things(v); + }); + std::thread thief([&]() + { + pop_thing(v); + }); + + owner.join(); + thief.join(); + } +} diff --git a/lesson_6/main.cpp b/lesson_6/main.cpp new file mode 100644 index 0000000..aa910e4 --- /dev/null +++ b/lesson_6/main.cpp @@ -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; +}