Advertisement
obernardovieira

[Timer] Independent callback&Independent args

Oct 18th, 2013
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. #include <thread>
  4. #include <functional>
  5.  
  6. void call(int numero) {
  7.     std::cout << numero << std::endl;
  8.     return;
  9. }
  10.  
  11. struct call_info{
  12.   std::function<void(void)> func;
  13. };
  14. class b_timers {
  15. public:
  16.     void start_timer() {
  17.        
  18.        
  19.         std::cout << "Chamou" << std::endl;
  20.  
  21.         std::this_thread::sleep_for(std::chrono::milliseconds(_milisegundos));
  22.         if(_active & 1) {
  23.             call_info& info = _myVec[0];
  24.             info.func();
  25.         }
  26.     }
  27.     void setProp(int milisegundos, std::function<void(void)> rnd) {
  28.         call_info info;
  29.         info.func = rnd;
  30.         _myVec.push_back(info);
  31.  
  32.         _active = true;
  33.         _milisegundos = milisegundos;
  34.     }
  35.     void setActive(bool set) {
  36.         _active = set;
  37.     }
  38. private:
  39.     bool _active;
  40.     int _milisegundos;
  41.     std::vector<call_info> _myVec;
  42. };
  43.  
  44.  
  45. void mt_timer(b_timers Timer) {
  46.     Timer.start_timer();
  47.     return;
  48. }
  49.  
  50.  
  51. int main() {
  52.     std::cout << "Inicio" << std::endl;
  53.  
  54.     std::function<void(void)> rnd = std::bind(call, 5);
  55.  
  56.     b_timers Timerx;
  57.     Timerx.setProp(2000,rnd);
  58.     std::thread thread_timer(mt_timer,Timerx);
  59.     thread_timer.detach();
  60.  
  61.  
  62.     system("pause");
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement