2021-10-10 20:46:11 +00:00
|
|
|
|
/*
|
|
|
|
|
* 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:
|
2021-10-10 20:48:41 +00:00
|
|
|
|
std::lock_guard<std::mutex> lg;
|
2021-10-10 20:46:11 +00:00
|
|
|
|
public:
|
2021-10-10 20:48:41 +00:00
|
|
|
|
pcout() : lg(std::lock_guard<std::mutex>(mtx_cout))
|
2021-10-10 20:46:11 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|