Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <chrono>
- #include <ctime>
- #include <set>
- class tmp {
- private:
- std::set<std::thread::id> ativo;
- int n;
- public:
- void print_n() {
- std::this_thread::sleep_for(std::chrono::seconds(1));
- if(ativo.find(std::this_thread::get_id()) != ativo.end()) {
- std::cout << n << std::endl;
- this->tmp::print_n();
- }
- }
- void stop(std::thread::id t) {
- ativo.erase(t);
- return;
- }
- void set(int v) {
- n = v;
- return;
- }
- std::thread::id print() {
- std::thread prt_thread(&tmp::print_n, this);
- ativo.insert(prt_thread.get_id());
- prt_thread.detach();
- return prt_thread.get_id();
- }
- };
- int main()
- {
- tmp TMP;
- TMP.set(5);
- std::thread::id the_thread = TMP.print();
- while(true) {
- int get;
- std::cin >> get;
- if(get == 0) {
- TMP.stop(the_thread);
- break;
- }
- TMP.set(get);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement