From 088984b93b504789096ade5654dca3399debc6fe Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Sun, 10 Oct 2021 23:46:11 +0300 Subject: [PATCH] added exercise 1 --- lesson_6/exercise_1.hpp | 53 +++++++++++++++++++++++++++++++++++++++++ lesson_6/main.cpp | 14 +++++++++++ 2 files changed, 67 insertions(+) create mode 100644 lesson_6/exercise_1.hpp create mode 100644 lesson_6/main.cpp diff --git a/lesson_6/exercise_1.hpp b/lesson_6/exercise_1.hpp new file mode 100644 index 0000000..ad56841 --- /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 lk; +public: + pcout() : lk(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/main.cpp b/lesson_6/main.cpp new file mode 100644 index 0000000..f84106c --- /dev/null +++ b/lesson_6/main.cpp @@ -0,0 +1,14 @@ +/* + * main.cpp + * + * Created on: 10 окт. 2021 г. + * Author: alexander + */ +#include "exercise_1.hpp" + +int main() +{ + exercise_1(); + + return 0; +}