lesson_6 #6

Open
alexander wants to merge 4 commits from lesson_6 into master
2 changed files with 62 additions and 1 deletions
Showing only changes of commit c00ee82f00 - Show all commits

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();
}
}

View File

@ -6,11 +6,13 @@
*/
#include "exercise_1.hpp"
#include "exercise_2.hpp"
#include "exercise_3.hpp"
int main()
{
// exercise_1();
exercise_2();
// exercise_2();
exercise_3();
return 0;
}