/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
#include <thread> // jthread еще не завезли, пришлось самому делать токен для отмены и вызвать join в деструкторе
#include <random>
#include <set>
#include <iomanip>
class MutableShit {
public:
int State() const {
return state;
}
bool operator<(const MutableShit& other) const {
return this->State() < other.State();
}
~MutableShit() {
killToken = true;
Thread.join();
}
private:
static int GenRand() {
std::random_device dev{};
std::default_random_engine gen{dev()};
std::uniform_int_distribution<int> range{0, 99};
return range(gen);
}
std::atomic<int> state = GenRand();
std::atomic<bool> killToken = false;
std::thread Thread{
[this](){
while (!killToken) {
state = GenRand();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
};
};
int main()
{
std::set<MutableShit> s;
for (int i=0; i<10; ++i) {
s.emplace();
}
for (int i = 0; i<5; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
for (const auto& el: s) {
std::cout << el.State() << " ";
}
std::cout << std::endl;
}
return 0;
}