RybaSG

Thread

Sep 16th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <thread>
  4. #include <unistd.h>
  5.  
  6. typedef struct Alarm
  7. {
  8.     std::string message;
  9.     int delay;
  10. }Alarm;
  11.  
  12. class Client
  13. {
  14.     private:
  15.         int x;
  16.         int y;
  17.         int z;
  18.         std::vector<std::thread> threads;
  19.  
  20.         std::vector<Alarm> alarms;
  21.  
  22.     public:
  23.         Client()
  24.         {
  25.  
  26.         }
  27.         ~Client()
  28.         {
  29.         }
  30.  
  31.         void printAlarm(std::string alarmString, int delay)
  32.         {
  33.             while(1)
  34.             {
  35.                 std::cout << alarmString << std::endl;
  36.                 std::this_thread::sleep_for (std::chrono::seconds(delay));
  37.             }
  38.         }
  39.  
  40.         void setAlarm(std::string alarmString, int delay)
  41.         {
  42.             std::cout << "Thread: " << alarmString << std::endl;
  43.             std::thread alarm(&Client::printAlarm, this, alarmString, delay);
  44.             threads.push_back(std::move(alarm));
  45.         }
  46. };
  47.  
  48.  
  49.  
  50. int main()
  51. {
  52.     Client cl;
  53.  
  54.     cl.setAlarm("GO AWAY", 1);
  55.     std::cout << "It doesn't work" << std::endl;
  56.     cl.setAlarm("AWAY GO", 3);
  57.    
  58.     int z;
  59.     std::cin >> z;
  60.     cl.setAlarm("DZIALA", 1);
  61.  
  62.     while(1);
  63.  
  64.  
  65.     return 0;
  66. }
Add Comment
Please, Sign In to add comment